State in local storage not restored after page reload with SSR
See original GitHub issueI’m using Next.js with SSR.
After saving my form data in local storage and refreshing the page, my form renders with the default values and not the values saved in local storage, although I have the option ssr enabled.
Here’s the relevant part of my code:
export default function createClient() {
const [
formInitialValues,
setFormInitialValues,
{ isPersistent, removeItem },
] = useLocalStorageState("awaji", {
ssr: true,
defaultValue: {
lastName: "",
firstName: "",
gender: "male",
dateOfBirth: "",
initialCredit: 10000,
},
});
const onSubmit = useCallback((values, actions) => {
console.log(values);
setFormInitialValues(values);
}, []);
// rest of the code...
Is there something I’m doing wrong ?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to Save State to LocalStorage & Persist on Refresh with ...
Step 3: Getting a stored value from localStorage and loading it into React state. Finally to make sure our state is persisted any...
Read more >Next.js - _app.js state is lost on page refresh / SSR
All state is kept when routing client side with the <Link> component. I'm trying to store non-sensitive data without using cookie/session/local ...
Read more >Can cause hydration mismatch with SSR #23 - GitHub
One issue I'm seeing for SSR'ed apps is if the stored value affects DOM ... to access data stored on localstorage on NextJS...
Read more >Using localStorage with React Hooks - LogRocket Blog
Learn how using localStorage with React Hooks can persist user information in browser storage and share logic between multiple components.
Read more >Persisting React State in localStorage - Josh W Comeau
A common thing in React development is that we want to store a bit of React state in localStorage, and re-initialize from that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@astoilkov you can close the issue. As far as I’m concerned I got it working so it’s fine for me. Thank you 😉
I’ve made a change in the new
use-local-storage-state
release (version 16) that can possibly fix your issue without the need forenableReinitialize
.If you are interested, you can upgrade and try it out. If you do, please let me know the result as I’m very curious.