Possible Unhandled Promise Rejection (id:0) TypeError: undefined is not an object

Possible Unhandled Promise Rejection (id:0) TypeError: undefined is not an object

我正在尝试在 React Native 项目 (android) 中使用 redux、thunk 和导航库实现登录逻辑,但我收到未处理的承诺拒绝 (id:0): (evalualting '_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'));
        })

您正在打电话 this._bootstrapAsync() 内部构造函数将它放在 componentDidMount

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

  }

componentDidMount() {
  this._bootstrapAsync();
}

....
}

动作道具没有return承诺。

此外,我建议您在 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'))
        });
}

不是一个好的做法return承诺发货。