可能的未处理的承诺拒绝(id:0)TypeError:undefined不是对象

时间:2019-06-06 05:49:40

标签: react-native react-redux react-native-navigation dispatch-async

我正在尝试在React Native项目(android)中使用Redux,Thunk和导航库实现登录逻辑,但我得到未处理的Promise拒绝(id:0):(评估'_this.props.navigation') 知道是什么原因导致此问题或出路了吗?

class AuthLoadingScreen extends React.Component {
  constructor() {
    super();
    this._bootstrapAsync();
  }

  _bootstrapAsync = async () => {
      this.props.getUserToken().then(() => {
        this.props.navigation.navigate(this.props.token.token !== null ? Devices' : 'UserAuth');
      }).catch(err => {
          this.setState({ err })
      })

  };

  render() {
    return (
      <View>
        <ActivityIndicator />
        <StatusBar barStyle="default" />
      </View>
    );
  }
}

// actionCreator.js
export const getUserToken = () => dispatch =>
 AsyncStorage.getItem('userToken')
        .then((data) => {
            dispatch(loading(false));
            dispatch(getToken(data));
        })
        .catch((err) => {
            dispatch(loading(false));
            dispatch(error(err.message || 'ERROR'));
        })

2 个答案:

答案 0 :(得分:0)

您正在打电话 构造函数中的this._bootstrapAsync()放在componentDidMount

class AuthLoadingScreen extends React.Component {
  constructor() {
    super();

  }

componentDidMount() {
  this._bootstrapAsync();
}

....
}

答案 1 :(得分:0)

动作道具没有返回承诺。

此外,我建议您在react-navigation-redux-helpers的帮助下在操作内调用导航。

在操作中使用导航。

export const getUserToken = () => dispatch => {
   AsyncStorage.getItem('userToken')
        .then((data) => {
            dispatch(loading(false));
            dispatch(getToken(data));
            dispatch(NavigationActions.navigate('successRoute'))
        })
        .catch((err) => {
            dispatch(loading(false));
            dispatch(error(err.message || 'ERROR'));
            dispatch(NavigationActions.navigate('failRoute'))
        });
}

返回承诺派遣的好习惯。

相关问题