Skip to content Skip to sidebar Skip to footer

Is Setting Value Of The Localstorage A Side Effect?

I'm working on a React+Redux app that must save certain values from the store to localStorage. I have some utility function, that securely store values to localStorage. I have an a

Solution 1:

As the other comment stated: yes, it's definitely a side effect. Now, it's probably not going to break anything in this case, but a reducer isn't the right place for that.

A better solution would be to either do this in a store subscription callback or a middleware. Or, even better, use one of the many store persistence libraries already available. Doing so in a thunk would also be an acceptable option, especially if you just want to persist a small amount of data rather than the entire store state.

Solution 2:

It's definitely a side effect in FP terms, but actually there is nothing bad with your code. There is no other better workaround of sharing your state between components (or even outside react.js tree) except redux/flux maybe.

Functional programming is a good thing but you don't need to turn that into cult

Post a Comment for "Is Setting Value Of The Localstorage A Side Effect?"