在 React Native 的 Flatlist 中使用来自自定义元素的参数进行导航:空参数

Navigation with parameters from custom element in Flatlist in React Native: Empty parameters

在未能成功解决我的问题 here 之后,我不得不创建一个新问题,其中包含我所有的 classes。

我是 React Native 的新手,在弄清楚如何通过传递参数从一个 class 导航到另一个 class 时遇到问题,非常感谢您的帮助。

我只想:

  1. 使用包含 CustomButton 的平面列表创建会话
  2. 通过单击 CustomButton 从 SessionCreate 导航到 ItemConfig
  3. 将参数“元素”传递给 ItemConfig
  4. 显示ItemConfig传入的参数内容

使用此设置,一个空的“元素”作为参数传递给 ItemConfigScreen(但没有发生错误):

app.js:

//Views

function Home({ navigation }) {
  return (
    <HomeScreen />
  );
}

function Session({ navigation }) {
  return (
    <SessionScreen />
  );
}

//subviews

function SessionCreate({ navigation }) {
  return (
    <SessionCreateScreen />
  );
}


function ItemConfig({ navigation }) {
  return (
    <ItemConfigScreen />
  );
}



//navigation stacks
const SessionStack = createStackNavigator();

function SessionStackScreen({ navigation }) {
  return (
    <SessionStack.Navigator>
      <SessionStack.Screen
        name="Session"
        component={Session}
        options={{ tabBarLabel: 'Session!'  }}
      />
      <SessionStack.Screen
        name="SessionCreate"
        component={SessionCreate}
        options={{ tabBarLabel: 'SessionCreate!' }}
      />
      <SessionStack.Screen
        name="ItemConfig"
        component={ItemConfig}
        options={{ tabBarLabel: 'ItemConfig!' }}
      />
      
    </SessionStack.Navigator>
  );
}



//Navbar Bottom
const Tab = createBottomTabNavigator();

function App() {
  return (
    <View style={[theme.colcontainer, { flexDirection: "column" }]} >
      
      <NavigationContainer>
        <Tab.Navigator
          screenOptions={({ route }) => ({
            tabBarIcon: ({ focused, color, size }) => {
              let iconName;

              if (route.name === 'Home') {
                iconName = focused ? 'home' : 'home-outline';
              } else if (route.name === 'Session') {
                iconName = focused ? 'book' : 'book-outline';
              } 

              // dynamic ionicon
              return <Ionicons name={iconName} size={size} color={color} />;
            },
          })}
       

        >
          <Tab.Screen name="Home" component={Home} />
          <Tab.Screen name="Session" component={SessionStackScreen} />
          
        </Tab.Navigator>
      </NavigationContainer>
    </View >
  );
}

export default App;

SessionScreen.js:

function SessionScreen() {
    const navigation = useNavigation();

    return (
        <View style={[theme.container, { flexDirection: "column" }]} >

                <View>
                    <TouchableOpacity onPress={() => navigation.navigate('SessionCreate')}>
                        <Text >Create Session</Text>
                    </TouchableOpacity>
                </View>

        </View >
    );
}

export default SessionScreen;

SessionCreateScreen.js:

    //data
    const sessionElements = [
        {
            id: "1",
            title: "title1"
        }
    ];
    
    
    function SessionCreateScreen() {
        const navigation = useNavigation()
    
        const renderItemConfiguration = ({ item }) => (
           <CustomButton element={item.title} onPress={() => navigation.navigate('ItemConfig', { element: 'item.title' })} />
 );
    
        return (
            
                <View style={{ flexDirection: "column", flex: 2}} >
                 
                    <SafeAreaView >
                        <FlatList
                            data={sessionElements}
                            renderItem={renderItemConfiguration}
                            keyExtractor={(item) => item.id}
                        />
                    </SafeAreaView>
    
                </View >
        );
    }
    
    
    
    export default SessionCreateScreen;

ItemConfigScreen.js:

const element = "";

function ItemConfigScreen() {

    return (
        <ScrollView >
            <View style={{ flexDirection: "column", flex: 2}} >
                <Text>Configure {element} here</Text>
            </View >
        </ScrollView>
    );

}

export default ItemConfigScreen;

感谢任何帮助。

要在 ItemConfigScreen 中获取参数,您必须使用 react-navigation 包中的 useRoute 挂钩。

您可以在此处阅读更多相关信息 useRoute

import {useRoute} from '@react-navigation/native';
function ItemConfigScreen() {

const route = useRoute();

const element = route.params?.element;

    return (
        <ScrollView >
            <View style={{ flexDirection: "column", flex: 2}} >
                <Text>Configure {element} here</Text>
            </View >
        </ScrollView>
    );

}


您在 CustomButton 中的 onPress 导航调用也有一个错误,您必须传递 ${item.title} 而不是 'item.title',然后只会传递实际数据。 JS Template Literals

<CustomButton element={item.title} onPress={() => navigation.navigate('ItemConfig', { element: `${item.title}` })} />