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.

Current recommended way to persist state?

See original GitHub issue

Hi,

First of all, I really enjoy using Recoil - looks like I finally found the best global state management tool for myself 😄

However, I don’t like a couple of things:

  • some pages in the documentation are outdated (see “State Persistence”);
  • the last release was 2 months ago.

I do understand that Recoil is still in the development phase, but some alpha/beta build releases (unstable of course) would be highly appreciated.


Now, my question is: what is the current recommended way to persist state? I saw atom effects, but these are not available yet (one of the reasons why unstable releases should be published). The useRecoilTransactionObserver_UNSTABLE hook has changed and does not work as shown on the “State Persistence” page.

Regarding the new behavior - I don’t work whether it’s possible to save the snapshot to for example the localStorage somehow 😕

I tried doing something like this to save the state:

useRecoilTransactionObserver_UNSTABLE(({snapshot}) => {
	const unit = snapshot.getLoadable(_unit).contents;
	const language = snapshot.getLoadable(_language).contents;
	const reports = snapshot.getLoadable(_reports).contents;

	localStorage.setItem('state', JSON.stringify({unit, language, reports}));
});

and then load it like so:

<RecoilRoot
	initializeState={({set}) => {
		if (state) {
			set(_unit, state.unit);
			set(_language, state.language);
			set(_reports, state.reports);
		}
	}}
>
	<Component {...pageProps}/>
</RecoilRoot>

This approach, unfortunately, doesn’t work - the state gets saved correctly but is not initialized.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
drarmstrcommented, Nov 1, 2021

Hey @drarmstr @bezi - did you get round to publishing that URL persistence library?

We have lots of PRs merging now in /packages/recoil-sync, though it hasn’t been published yet. This approach initializes atoms, so it should be compatible with SSR.

2reactions
hellosmithycommented, Dec 22, 2020

@drarmstr any updates on the URL persistence library? This would be really useful even if still quite alpha.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Methods to Persisting State Between Page Reloads in React
One of the straightforward options is to use localStorage in the browser to persist the state. Let's take a look at an example....
Read more >
Persist state with Redux Persist using Redux Toolkit in React
Specify how the incoming state is merged. Merging involves saving the persisted state back in the Redux store. When our app launches, our ......
Read more >
Persisting State in React Apps - Medium
Persist React's useState to localStorage is a common requirement. You'd want to persist user's preferences or data to have them at hand on ......
Read more >
Nine Options for Managing Persistent User State in ASP.NET
ASP.NET provides many different ways to persist data between user requests. You can use the Application object, cookies, hidden fields, the ...
Read more >
3 Ways to Persist Pinia State - YouTube
A common use case for all data stores is the need to persist data. In this video, we'll see a few different approaches...
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