Search code examples
react-nativereact-reduxreact-native-navigationdispatch-async

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


I'm trying to implement login logic using redux, thunk, and navigation libraries in react native project (android) and I get unhandled promise rejection (id:0): (evalualting '_this.props.navigation') any idea whats causing this problem or way out?

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'));
        })

Solution

  • You are calling this._bootstrapAsync() inside constructor place it in the componentDidMount

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