Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to use localStorage instead of Redux or Context API?

I've been working with React for a little while, and after some time i started to ask myself, why not store every piece of data that my components need to share in localstorage instead of using Redux. Redux need so much boilerplate while localstorage is so simple. You can say that you cant storage object in localstorage but you can JSON.stringfy them, and on recovery just parse them back. So problaby there's something that i cant see properly about that, cause Redux is largely used and localstorage is normally used just to save data you dont wish to loss on refresh and things like that.

like image 432
GBenitez Avatar asked Jun 03 '20 13:06

GBenitez


People also ask

What is the difference between Redux and localStorage?

The purpose of both are different. Redux is for state management. Local storage is for saving some stuff for a long time on client side. If you're talking about saving state in local storage,then it's not good because anybody can inspect local storage using devtools and possibly modify it.

Is it safe to use local storage in Javascript?

No. localStorage is accessible by any webpage, and if you have the key, you can change whatever data you want. That being said, if you can devise a way to safely encrypt the keys, it doesn't matter how you transfer the data, if you can contain the data within a closure, then the data is (somewhat) safe.


3 Answers

This question was on my head when I started developing react apps. There are many reasons than below to use redux over localStorage. but at least

  1. Using Redux alongside react-redux is not only for store data. don't forget that changing in a state will reRender All components that listen to that state. and that is really what react-redux does.
  2. stringify/parse from localStorage will make your app (on high-scale) slower and also will not sync all components while changing state.

Personal Recommendation After more than 4 years of developing React Apps, use REDUX with easy API like redux-toolkit or rematch

like image 173
Maged Mohamed Avatar answered Oct 17 '22 22:10

Maged Mohamed


Redux and localStorage have different use cases actually. Redux you'll use to manage your application state across multiple components.

Local Storage you'll use to persist properties in the browser for later usage. The problem is that any change on your localStorage won't reflect on your application. You'll have to do it manually.

like image 8
Daniel Cunha Avatar answered Oct 17 '22 23:10

Daniel Cunha


The purpose of react-redux is to allow other components to connect to state and then react on changes. You are loosing the whole scope of using react-redux/context api.

like image 2
Iosif Bujor Avatar answered Oct 17 '22 22:10

Iosif Bujor