Anas Nisar
Anas Nisar

Reputation: 104

Passing arguments to action

I am trying to pass an object as an argument to a user action but I can't access it in the action. It just returns undefined when I do console.log(dataToSubmit). Here is the action method:

const loginUser = (dataToSubmit) => {
    console.log(dataToSubmit)
    return (dispatch) => {
        dispatch(loginUserRequest)
        axios.post('http://localhost:5000/api/user/login' , dataToSubmit)
        .then(response => {
            if(response.data.loginSuccess){
            const user = response.data.user
            dispatch(loginUserSuccess(user))
            }
            else{
                console.log('Login attempt failed')
            }
        })
        .catch(error => dispatch(loginUserFailure(error.message)))
    }
}
export default loginUser

I'm passing the argument here:

  const handleSubmit = (event) => {
    event.preventDefault();
    if (isFormValid(email , password)){
      const data = { 
        email,
        password
      }
      loginUser(data)
    }

  }

I've already mapped dispatch to props:

const mapDispatchToProps = (dispatch) => {
  return{
      loginUser : () => dispatch(loginUser())
  }
}


export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SignIn)

I'm new to redux. Thanks

Upvotes: 0

Views: 21

Answers (1)

Eason
Eason

Reputation: 518

You need to pass the argument in mapDispatchToProps

const mapDispatchToProps = (dispatch) => {
  return{
    loginUser : (data) => dispatch(loginUser(data))
  }
}

Upvotes: 1

Related Questions