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.

Manipulating Recoil state in big asynchronous functions

See original GitHub issue

When 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:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Apr 12, 2021

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.

0reactions
nxz91commented, Apr 13, 2021

Got it, thanks!

Read more comments on GitHub >

github_iconTop 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 >

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