首页 > 解决方案 > 通过反应导航将参数传递给路由但结果未定义

问题描述

嗨,我是新来的本地人,并试图学习一些东西。我正在尝试从 rest API Laravel 传递数据。这是我的代码

if (responseData.items) {
          this.props.navigation.push('SearchResults', {users: responseData.items });
          } else {
            this.setState({ errorMessage: 'No results found'});
          }

我的代码导航器

const Sp2hpStack = createStackNavigator({

搜索:SearchScreen,SearchResults:SearchResults,});

我的代码结果

render() {
const { users } = this.props.navigation.state;
console.log(users);
return (
  <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
    <Text>email : {users.email} </Text>
  </View>
);

}

但结果未定义请帮助我

标签: reactjsreact-nativereact-navigation

解决方案


要从路由中获取参数,您可以通过this.props.navigation.getParam('users', [])访问它们

PS:空数组是可选的,如果用户未定义,这将是默认值


推荐阅读