Gonzalo4488
Gonzalo4488

Reputation: 449

When a function inside my actions is called it won't work unless I take out the dispatch

I'm working with React Redux, and I'm trying to call a function inside another function on my actions file. The problem is It only work if I take the dispatch part of the function.

I wan't to call updateToken() from askForUser() but it will only work if I take the dispatch part from updateToken(). Also when i call askForUser() from askForUser() it won't work either.

const updateToken = (token) => dispatch => {
    console.log(token)

    dispatch({
        type: UPDATE_TOKEN,
        payload: token
    })
}


const askForUser = (token) => dispatch => {
    dispatch({ type: ASKING_FOR_DATA })
    axios.get(API.GET_USER, {
        params: {
            token
        }
    }).then((response) => {
        if (response.data.status === 1) {
            dispatch({ type: ASK_FOR_USER, payload: response.data.user })
        } else if (response.data.status === 2) {
            updateToken(response.data.token)
            //console.log(hola2())
        } else {
            NotificationManager.error('Error consiguiendo usuario')
            dispatch({ type: ERROR_HANDLER })
        }
    }).catch((err) => {
        console.log(err)
        dispatch({ type: ERROR_HANDLER })
    })
}

Upvotes: 1

Views: 25

Answers (1)

Taki
Taki

Reputation: 17654

you'll have to dispatch it :

 else if (response.data.status === 2) {

   dispatch(updateToken(response.data.token)); // dispatch the function here

 }

Upvotes: 1

Related Questions