question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State in local storage not restored after page reload with SSR

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Bradzercommented, Feb 24, 2022

@astoilkov you can close the issue. As far as I’m concerned I got it working so it’s fine for me. Thank you 😉

0reactions
astoilkovcommented, Mar 14, 2022

I’ve made a change in the new use-local-storage-state release (version 16) that can possibly fix your issue without the need for enableReinitialize.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found