【问题标题】:passing params to routed with react-navigation but result undefined将参数传递给使用 react-navigation 进行路由但结果未定义
【发布时间】:2018-11-14 15:14:39
【问题描述】:

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

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

我的代码导航器

const Sp2hpStack = createStackNavigator({

搜索:搜索屏幕, 搜索结果:搜索结果, });

我的代码结果

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>
);

}

但结果未定义 请帮帮我

【问题讨论】:

    标签: reactjs react-native react-navigation


    【解决方案1】:

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

    访问它们

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

    【讨论】:

    • 相同的结果未定义
    • 好的,你能添加整个 SearchResults 组件的代码吗?
    【解决方案2】:

    请尝试使用 - const { users } = this.props.navigation.state.params; 而不是 const { users } = this.props.navigation.state;

    【讨论】:

    • 相同的结果未定义
    • 请确保 responseData.items 不是未定义的。导航器也应该如下:const Sp2hpStack = createStackNavigator ({Search: {screen: SearchScreen},SearchResults: {screen: SearchResults},});
    • 问题解决了我使用console.log(users[0].email);
    猜你喜欢
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2020-09-07
    • 2021-01-05
    • 2019-04-28
    相关资源
    最近更新 更多