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:
'state'
is retrieved and set, use this.state['uid']
to sync state with Firebase (fetching), then update local storage state again.'state'
item.Example #2:
'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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With