【问题标题】:Route not updating state from navigation params路由未从导航参数更新状态
【发布时间】:2021-02-02 12:10:13
【问题描述】:

所以我有这个显示产品详细信息的屏幕,它像模板一样工作,因为只有来自导航参数的数据发生变化,我遇到问题是因为没有重新加载,返回并再次安装时它工作正常,那不是这里的情况,因为我在同一个屏幕上显示了相关产品,我需要一种方法来重新加载当前路线或更新状态

我已经用 console.log 检查过,第二次点击没有任何显示

constructor (props) {
  super(props)
  this.state = {
    product: this.props.route.params.product,
    id: this.props.route.params.product.id,
  }
}

导航到我在屏幕本身或其他路线上使用的路线

viewProduct = (product) => {
  this.props.navigation.navigate('SingleProduct', { product: product })
}

我在componentDidMountUNSAFE_componentWillReceiveProps 中都尝试过 setState,但结果仅在再次点击后才会显示

【问题讨论】:

标签: javascript reactjs react-native


【解决方案1】:

您可以使用以下函数从 singleProduction 屏幕导航到具有不同参数的同一屏幕。

this.props.navigation.replace('SingleProduct', { product: product })

【讨论】:

    【解决方案2】:

    直接使用route.params?.product 并在useEffect 数组中提及更改。

      useEffect(() => {
    
        return () => {
          // Clean up the subscription
          
        };
      // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [route.params?.product]);
    

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2015-03-26
      • 2018-12-13
      • 1970-01-01
      • 2023-01-18
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多