Current recommended way to persist state?
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top GitHub Comments
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.@drarmstr any updates on the URL persistence library? This would be really useful even if still quite alpha.