首页 > 解决方案 > 如何在反应原生中从导航中获取参数值

问题描述

 this.props.navigation.navigate("ProductDeatils",
      {params:{ price: this.state.GridListItems.price,
        url: this.state.GridListItems.url} 
      });
I wanted to display these params values on the "ProductDeatils" page. please can anyone help me?

this.props.navigation.params 未定义。我试过这个 const { navigation } = this.props; const id = navigation.getParam('price') 不工作

标签: reactjs

解决方案


  1. 确保this指向组件而不是函数本身。

  2. 哪个版本的反应导航?你配置NavigationContainer好了Stack.NavigatorStack.Screen

例子:

应用程序.js

      <NavigationContainer>
        <Stack.Navigator
          initialRouteName="Splash"
          screenOptions={{
            headerShown: false,
          }}>
          <Stack.Screen name="Login" component={Login} />
         .......

登录.js

export default class Login extends React.Component {
  componentDidMount() {
     console.log(this.props.navigation) // It should work
  }
 .....
}


推荐阅读