使用Reaction Native:Empty Parameters中的FlatList中的自定义元素中的参数导航 [英] Navigation with parameters from custom element in Flatlist in React Native: Empty parameters

查看:24
本文介绍了使用Reaction Native:Empty Parameters中的FlatList中的自定义元素中的参数导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未能成功解决问题here后,我必须创建一个包含所有类的新问题。

我刚开始进行本机反应,在找出如何使用传递参数从一个类导航到另一个类时遇到问题,非常感谢您的帮助。

我想做的就是:

  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中的参数,您必须使用Reaction导航包中的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}` })} />

这篇关于使用Reaction Native:Empty Parameters中的FlatList中的自定义元素中的参数导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆