使用react-navigation 5.x在两个全状态组件之间传递参数。

问题描述 投票:0回答:1

我试图在我的应用程序中的两个全状态组件(餐厅ID)之间传递一个参数,第一个是我的登录组件,我这样做。

  onPressLogin = () => {
    loginService(this.state.email, this.state.password)
      .then((res) => {
        this.props.navigation.navigate('Commandes', {id:res.data.restaurant.id})
      })
      .catch((err) => {
        console.log(err)
        alert('Email/Password invalide')
      })
  }

而在我的第二个组件,主屏幕(屏幕:Commandes),我这样做。

componentDidMount() {
    const {id} = this.props.route.params
    this.getRestaurantInfo()
    this.getRestaurantStatus()
    this.getOrders()
    this.setState({ isLoading: false })
  }

但我有未定义,我检查了文档,他们只为无状态组件指定了......

我还检查了this.props.navigation.navigate的值,这是一个函数,以及显示这个路由的值。

route.params Object {
  "key": "Commandes-kHfSvHgETC",
  "name": "Commandes",
  "params": undefined,
}
react-native react-navigation
1个回答
0
投票

你可以试试这个

this.props.navigation.state.params.id

0
投票

请检查 https:/reactnavigation.orgdocsparams。 再出来,确保你做得很好

© www.soinside.com 2019 - 2024. All rights reserved.