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.

Subscribing effects to state

See original GitHub issue

I’m wondering if there’s plans to allow functionality such as subscribing effects to state. Let me be a bit more specific in what I mean - I’m trying to store a certain atom’s value to localstorage, and right now the best solution I’ve come up with is this:

Declaring my atom like so:

const myState = atom({
  key: 'myState',
  default: JSON.parse(window.localStorage.getItem('myState')) || myDefaultValue,
});

And writing this in my root component:

const myStateValue = useRecoilValue(myState);
useEffect(() => {
  window.localStorage.setItem('myState', JSON.stringify(myStateValue));
}, [myStateValue]);

But it would be really nice if we could specify some kind of subscribed function somewhere that just runs the localstorage setter on value change, so we don’t have to rely on the render of the root component. As an example API:

const myState = atom({
  key: 'myState',
  default: JSON.parse(window.localStorage.getItem('myState')) || myDefaultValue,
  subscriber: value => window.localStorage.setItem('myState', JSON.stringify(value)),
});

If this functionality already exists and I’m just missing it, please feel free to point it out and close the issue. Otherwise I’d love to hear the team’s thoughts on such functionality!

Issue Analytics

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

github_iconTop GitHub Comments

10reactions
drarmstrcommented, May 16, 2020

There is a useTransactionObservation_UNSTABLE hook you can use to subscribe to state changes for managing persistence. There is also a persistence_UNSTABLE option for atoms to add metadata, such as a validator function. It’s listed as _UNSTABLE as we’re still finalizing the API, but we use this internally. We’re working now to clean up this API and publish an example persistence library.

5reactions
drarmstrcommented, Jun 16, 2020

@Qrysto @Joonpark13 - Yup, stay tuned for “Atom Effects” coming soon…

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngrx How to subscribe to action in component ... - Stack Overflow
i put object to state by action via effect: ... Subscribe to store and get state just when "newField" already in place. @Effect() ......
Read more >
[TUTORIAL] How to Make a Basic Subscribe Animation
Make sure to subscribe and like the video if you enjoyed it! ... after effects, subscribe animation after effects tutorial,youtube subscribe ...
Read more >
Using the Effect Hook - React
They let you use state and other React features without writing a class. The Effect Hook lets you perform side effects in function...
Read more >
Observables, Side-effects, and Subscriptions | Eyas's Blog
Say I have two Observables wrapping some object in a storage format (e.g. JSON), and I'd like to display it. Unpacking an observable...
Read more >
U.S. State Laws Impose New Obligations for Businesses ...
Several similar state ARL requirements have also come into effect on January 1, ... Automatic renewals, which means paid subscriptions 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