Wasn't able to successfully use solutions from past posts.
I'm trying to run an action every time the app loads using useEffect on app.js like this :
26 | const App = () => {
27 |
28 | useEffect(() => {
> 29 | store.dispatch(loadUser());
30 | }, []);
31 |
32 | return (
The error i'm getting :
Error: Actions must be plain objects. Use custom middleware for async actions.
The action :
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
})
})
}
What am I doing wrong?
loadUserreturns a function, not an object. Seems like you havent configured redux-thunk properlyreturnErrorsan action?