Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I persist state in local storage React without Redux?

I'm building a web app in React connected to Firebase, which authenticates users and stores post data. There seems to be a lot of documentation online about using Redux, but not a whole lot on persisting state without it.

When a user refreshes a page, the <App />'s State is reset, so I need to preserve the existing state/retrieve it from Firebase. Some articles suggest using local storage to store state. But with best practices in mind, what should be stored in local storage? My app stores all it's state in <App />, and passes down needed state as props to relevant components.

The following two examples assume a user has authenticated already and has just refreshed their page. We want their session to be preserved, so do we:

Example #1:

  • App loads and checks local storage for a stringified object of the last stored local state. Sets the state to either that or empty state.
  • If 'state' is retrieved and set, use this.state['uid'] to sync state with Firebase (fetching), then update local storage state again.
  • Whenever a user updates something, update the state (which updates Firebase) and also update the local storage state.
  • When user logs out, clear the local storage 'state' item.

Example #2:

  • App loads and checks local storage for a uid (which is set during authentication), if one is found, uses that to sync state with firebase (fetch).
  • If a user reloads a page, data is fetched again from Firebase.
  • When user logs out, clear the local storage 'uid' item.

With the first example, I'm effectively keeping two copies of the state. One in React's State, and another in the browser's Local Storage. In case the user closes the window or refreshes the page.

My question is: Which is the best practice?

or, is building a React app that needs to persist user and state across different routes without Redux totally bonkers?

Learning React is enough of a curve without Redux, so I'm trying to see if I can manage without it.

like image 389
5tormTrooper Avatar asked Sep 24 '17 10:09

5tormTrooper


People also ask

Can I use localStorage instead of redux?

The answer is in your question, yes local storage is only used for storing data in the browser while redux and context api solve some different problem. It separates your data layer from your view to easily manage your data state. If the app is not really big then you should consider going with Context API.

How save state in local storage React?

You can initialize your state by getting the value of local storage. useState hook accept a function as a parameter. Now, when your component would render, it will get the value that is on local storage and assign to your state. But, it would be easier if you would use React Context to manage global values .

How do you persist the state in React?

Persisting the state of React applications is a great way to contribute to a more cohesive user experience by saving the user's progress or settings—like the dark-mode setting on this page. By saving data in local storage, the user doesn't have to start over or adjust the settings on subsequent visits.


1 Answers

In your use case, redux is not gonna help you because redux's state also resets when user refreshes the page. If you want to keep the state of your app on refresh/browser termination, then you need to use localStorage/sessionStorage/cookies etc.

Which is best way?

Well I would say that the second approach is better. I would avoid keeping copy of state in localstorage. I would only keep some kind of token/id (in your case uid) which uniquely identify the user and would fetch fresh data every time. When your application grows it can be hard to manage those states in localstorage.

like image 90
Prakash Sharma Avatar answered Oct 08 '22 03:10

Prakash Sharma