gpt4 book ai didi

javascript - Redux 不会通过调度调用另一个函数

转载 作者:行者123 更新时间:2023-12-02 21:31:39 24 4
gpt4 key购买 nike

在 redux-thunk 中调用调度后,我在调用函数时遇到问题。它调用“console.log”等常规函数,但忽略上面声明的函数。我被困住了,不知道如何解决这个问题。

操作

export const loadUser = () => async (dispatch) => {
if(localStorage.token) {
setAuthToken(localStorage.token)
}

try {
const res = await axios.get('/auth')

dispatch({
type: USER_LOADED,
payload: res.data
})
} catch(err) {
dispatch({
type: AUTH_ERROR
})
}
}


export const login = (formData) => async(dispatch) => {
const config = {
headers: {
'Content-Type' : 'application/json'
}
}

try {
const res = await axios.post('/auth', formData, config)

dispatch({
type: LOGIN_SUCCESS,
payload: res.data
})

loadUser() // THIS FUNCTION IS IGNORED
console.log('test') // but this one is called.
} catch(err) {
dispatch({
type: LOGIN_FAIL,
payload: err.response.data.msg
})
}
}```

最佳答案

loadUser() 返回一个 thunk,因此您需要分派(dispatch)它:

dispatch(loadUser());

关于javascript - Redux 不会通过调度调用另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60609908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com