reactjs - Trying to save data in localStorage -
in react app, after fetching data api, i'm trying store data in localstorage redux store. keep getting
error: actions must plain objects. use custom middleware async actions.
here's code:
export const setdatainstore = (mydata) => { return { type: types.set_my_data, mydata }; } const setdatainlocalstorage = (mydata) => { window.localstorage.mydata = mydata; } export const getmydata = () => { return (dispatch) => fetch('/api/mydata', fetchoptionsget()) .then((response) => { if (response.ok) { parsejson(response) .then(mydata => { dispatch(setdatainstore(mydata)); dispatch(setdatainlocalstorage(mydata)); }) } else { // handle error } }) }
the issue setdatainlocalstorage
function. when put debuggers
, see i'm coming setdatainlocalstorage
, mydata
populated api call.
what doing wrong there?
dispatch
expects action object returned. in case setdatainlocalstorage
doesn't return action dispatch
with.
since want set localstorage, can change dispatch lines to:
dispatch(setdatainstore(mydata)); setdatainlocalstorage(mydata);
we treat setdatainlocalstorage() regular function being called.
Comments
Post a Comment