无法成功使用过去帖子中的解决方案。
每次useEffect在 app.js 上加载应用程序时,我都尝试运行一个操作,如下所示:
26 | const App = () => {
27 |
28 | useEffect(() => {
> 29 | store.dispatch(loadUser());
30 | }, []);
31 |
32 | return (
我得到的错误:
Error: Actions must be plain objects. Use custom middleware for async actions.
那个行动 :
export const loadUser = () => (dispatch, getState) => {
dispatch({ type: USER_LOADING})
const token = getState().auth.token
const config = {
headers : {
"Content-type": "application/json"
}
}
if (token) {
config.headers["x-auth=token"] = token
}
axios.get("/api/auth/user", config)
.then(res => dispatch({
type: USER_LOADED,
payload: res.data
}))
.catch(err => {
dispatch(returnErrors(err.response.data, err.response.status))
dispatch({
type: AUTH_ERROR
})
})
}
我究竟做错了什么?
茅侃侃
胡子哥哥
慕村225694
相关分类