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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -