1

In our project, I was need to sync a piece of information between localStorage and the Redux state. Actually I subscribed to changes in a localStorage and in Redux, and if change happens in localStorage I update the state, and vice versa.

Right now I've created a redux enhancer for that. My coworker, however, claims that it isn't enhancing store functionality, and recommends to use a component, a content-provider or a fragment for that.

As doing it inside Redux' reducers is not recommended, as the reducers should be pure functions, where am I expected to put this sort of side effects?

3
  • 1
    1. Where exactly your colleague suggests to put this code instead of the enhancer? 2. Do you synchronize everything in the store, or just specific pieces of information from the store? Dec 31, 2019 at 16:55
  • In a Component, Content-Provider or Fragments . It should sync only JWT token. I must admit, that I relatively new in a react, only one month. In Vue, I would has done this functionality in an Action. But according to Dan Abramov, Redux' Reducer must be pure functions, so it should be done in other place. stackoverflow.com/questions/35305661/…
    – Anatoly
    Dec 31, 2019 at 17:38
  • Indeed, reducers shouldn't have side effects. There are a bunch of articles such as this one explaining where to put code which has side effects. I'm sorry, I don't have enough skills in React-Redux to be able to tell if those articles are correct or not, neither to write an answer to your question. I'm also surprised I can't find any answer on the subject here, on SE.SE. I therefore reworded your question a bit to make it more specific and reduce the risk of being closed/downvoted. Dec 31, 2019 at 17:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.