Manipulating Recoil state in big asynchronous functions
See original GitHub issueWhen writing asynchronous functions, we would like to get and set Recoil state along the way. The problem with simply using useRecoilCallback with asynchronous functions is that snapshot.getPromise() will return the value from the time function execution began, and not when snapshot.getPromise() is called. To find out what the current value of a Recoil key is, we were thinking the following could work, but then we were wondering if this is discouraged for any reason, because no such helper function exists:
const getRecoilInterface = useRecoilCallback((callbackInterface) => () =>
callbackInterface
)
You can then just use getRecoilInterface().snapshot.getPromise() to get the current value of any atom and getRecoilInterface().set() to set any atom.
It looks like a very simple solution, but is there anything that speaks against this?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Asynchronous Data Queries - Recoil
Recoil provides a way to map state and derived state to React components via a data-flow graph. What's really powerful is that the...
Read more >Update Recoil state from async function - Stack Overflow
I've find a solution by self : I've created a 'selector' from my tasks 'atom', and delegated in a custom 'set' method the...
Read more >Exploring Asynchronous Requests in Recoil - AppSignal Blog
Recoil hooks into this React component when the state is asynchronous. The library fails to compile if an async request is not wrapped...
Read more >Use Async Functions in a Recoil Selector - Egghead.io
1. Set Up Recoil in a new React App. 1m 27s · 2. Create Shared State with Recoil in a React App ·...
Read more >Using Recoil instead of Redux For State Management In ...
Recoil is an open-source state management library with more than 14k stars on Github, it was invented by Dave McCabe, a Software Engineer...
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
This “recoil interface” can work for getting the “current” value at the time the callback is called for that React context and it’s possible for advanced cases where you’re sure what you’re doing. But, we don’t want to broadly encourage it necessarily. We want to default to users working with consistent state. For example, in a callback reading state A, then doing something, then reading state B. The state A and B should be consistent and legal to both be in that state, which is the case when you look at the same snapshot for both of them based on the state when the callback was issued. If it defaulted to getting state B from some newer snapshot, then A and B may have incompatible values. That approach has led to bugs.
Got it, thanks!