【问题标题】:React Native: componentWillReceiveProps was deprecated, what should i use to navigateReact Native:componentWillReceiveProps 已被弃用,我应该使用什么来导航
【发布时间】:2018-10-28 04:19:24
【问题描述】:

我正在使用 componentWillReceiveProps 接收下一个状态并在收到某些状态时导航到新屏幕。

//     if(this.props.success !== prevProps.success && this.props.success) {
//         this.props.navigation.navigate('Attendances');
//     }
//     else if (this.props.error !== prevProps.error && this.props.error) {
//         alert(this.props.errorMessage);

由于新函数 GetDerivedStatesFromProps 是一个静态函数,所以我不能使用 this.props.navigation.navigate 进入下一个屏幕。

什么或如何获得相同的结果。

【问题讨论】:

    标签: react-native routing navigation state


    【解决方案1】:

    使用componentDidUpdate:

      componentDidUpdate(prevProps) {
         if(this.props.success !== prevProps.success && this.props.success) {
           this.props.navigation.navigate('Attendances');
         } else if (this.props.error !== prevProps.error && this.props.error) {
           alert(this.props.errorMessage);
         }
     }
    

    我也不确定你为什么调用你的方法参数prevProps,因为componentWillReceiveProps实际上接收到nextProps

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 2019-10-01
      • 2020-01-18
      相关资源
      最近更新 更多