D
D
d1zz742021-05-20 15:49:13
React
d1zz74, 2021-05-20 15:49:13

How to call action when needed?

There is an action login that is called on onSumbit:

const onSubmit = data => {
        console.log(data);
        dispatch(login(data));
    }


And the action itself:
export function login(data) {
    return dispatch => {
      return axios.post('http://localhost:5000/api/auth/login', data).then(res => {
            console.log(res)
            const token = res.data.token;
            localStorage.removeItem('jwtToken');
            localStorage.setItem('jwtToken', token);
            dispatch(loadUser(token));
        })
        .catch(function (error) {
            dispatch(returnErrors(error.message, error.status, "signIn"))

        });
    }
}


But the thing is, when I go to the page where the form is, this action is called, but since there is no data when the catch block itself is called.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d1zz74, 2021-05-20
@d1zz74

Used mapDispatchToProps and everything is ok now.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question