Question: How to get latest value from snapshot.getPromise() when update state very fast
See original GitHub issueHi, I have found that if I use useRecoilCallBack to update the state very fast, the snapshot.getPromise always seems to mess up the first transaction.
The example is in https://codesandbox.io/s/objective-williams-9gn7k?file=/src/App.js
export const countState = atom({
key: "count_key",
default: 1
});
const Test = () => {
const handleIncrease = async () => {
let i = 0;
while (i < 10) {
await increase();
i++;
}
};
const increase = useRecoilCallback(
({ set, snapshot, gotoSnapshot }) => async () => {
const currentPromise = await snapshot.getPromise(countState);
console.log("Snapshot Value: ", currentPromise);
set(countState, value => {
console.log("inside set", value);
return value + 1;
});
}
);
return (
<p>
<button onClick={handleIncrease}>Increase Counter</button>
</p>
);
};
I’m not sure how to avoid this.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Can't update state immediately? - Stack Overflow
" When I add/delete data to rejected order array that's mean getRejectedOrders() just will change but fetchOrders() not because it's not related ...
Read more >Updating Objects in State - React Docs
State can hold any kind of JavaScript value, including objects. But you shouldn't change objects that you hold in the React state directly....
Read more >How To Manage State on React Class Components
You'll learn how to pass a new object containing updated values to a special method called setState , which will then set the...
Read more >NetSuite Applications Suite - Oracle Help Center
Understanding How the Nexus is Determined on Transactions in SuiteTax · Nexus Determination Lookup Logic in SuiteTax.
Read more >useRecoilCallback(callback, deps) | Recoil
This hook is similar to useCallback() , but will also provide an API for your ... Snapshot of Recoil state and the ability...
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
The snapshot for
useRecoilCallback()
is from the currently committed state when the the callback was initiated, while the updaters provide the current state that is pending for the next batch. This state may change as subsequent sets or updates are performed before the batch is committed. We should avoid assumptions about when React batches are run. So, use either approach depending on what consistency guarantees you need, either with the latest pending updates or with the latest committed batch.yes i understand. but please consider advanced users. i try to explain better here https://github.com/facebookexperimental/Recoil/issues/829