1

I'm using Redux Promise Middleware and my action creator returns an action with a promise.

Now, in the reducer, I receive the resolved promise data and I have to 'provide' the new state. I also have to do something that is not 'pure' with that data, e.g., store it in local storage.

How should I approach this situation and still keep the reducer pure? That data is obtained in middleware and the first time I get it is in reducer.

I know I can handle the promise in my action creator and access the data before dispatching the action but is there another better way?

Patrick Burtchaell
  • 4,112
  • 1
  • 14
  • 25
bobocu
  • 11
  • 1

1 Answers1

3

You can create your own middleware!

You can call it localStorage-middleware for instance and you can dispatch the action before providing your data to the reducer. Your middleware will listen to that action, like dispatch(saveDataToLocalStorage(dataToSave))

Ematipico
  • 1,181
  • 1
  • 9
  • 14