Subscribing effects to state
See original GitHub issueI’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:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top 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 >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
There is a
useTransactionObservation_UNSTABLE
hook you can use to subscribe to state changes for managing persistence. There is also apersistence_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.@Qrysto @Joonpark13 - Yup, stay tuned for “Atom Effects” coming soon…