Ruan Duarte
Ruan Duarte

Reputation: 385

TypeError: dispatch on action execution

In this my action the user's log out of my application is exported in react

export const logoutUser = () => {
  return dispatch => {
    dispatch(
      {
        type: LOGOUT_USER
      }
    )
      .then(() => {
        logoutUserSuccess(dispatch, )
      })
      .catch((err) => {
        logoutUserError(err, dispatch)
      })
  }
}

const logoutUserSuccess = (dispatch) => {
  dispatch(
    {
      type: LOGOUT_USER_SUCCESS
    }
  )
  AsyncStorage.removeItem('@token_jwt')
  console.log('saiu')
  Actions.loginScreen()
}

const logoutUserError = (err, dispatch) => {
  dispatch(
    {
      type: LOGOUT_USER_ERROR
    }
  )
  Alert.alert('Erro ao sair da conta')
  console.log(err)
}

is my Reducer

case LOGOUT_USER:
return {
    ...state
}
        case LOGOUT_USER_SUCCESS:
return {
    INITIAL_STATE
}
        case LOGOUT_USER_ERROR:
return {
    ...state
}

is my screen to logout

 onLogout() {
      this.props.logoutUser()
  }

const mapStateToProps = state => (
  {
    email: state.Authentication.email
  }
)

export default connect(mapStateToProps, { logoutUser })(Home)

The return is the following error

Erro

I put the email on the mapStateToProps, because I don't know how to leave it blank, what matters to me is the logout

Upvotes: 0

Views: 31

Answers (1)

Ajin Kabeer
Ajin Kabeer

Reputation: 2186

You can try creating a mapDispatchToProps function and dispatch the action logoutUser from inside the function and pass it as a second argument to connect.

In doing so, you can invoke the LogoutUser from mapDispatchToProps in your onLogout function.

import {logoutUser} from './youractions.js'

onLogout() {
  this.props.LogoutUser();
}

const mapDispatchToProps = (dispatch) => ({
  LogoutUser: () => dispatch(logoutUser()),
});

export default connect(null, mapDispatchToProps)(Home);

Upvotes: 1

Related Questions