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.

Question: How to get latest value from snapshot.getPromise() when update state very fast

See original GitHub issue

Hi, 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>
  );
};

image

I’m not sure how to avoid this.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
drarmstrcommented, Jul 16, 2020

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.

0reactions
salvoravidacommented, Jan 12, 2021

As the name implies, the “snapshot” is a static immutable snapshot of what the committed atom state was when the callback was initiated. If you want the latest state for an atom or selector, particularly during or after async calls, then you should use the updater form of the setter to get the most-up-to-date state that includes queued pending state changes. This is particularly important as we start working with React’s new Concurrent Mode.

i @drarmstr why not expose get on useRecolCallback? function get(recoilState) { return getRecoilValueAsLoadable(storeRef.current, recoilState); }

This example gets a Loadable, not the value itself. getLoadable() is exposed in the Snapshot API. One reason the snapshot provides the snapshot values at the time the callback was initiated vs the current value is to avoid problems with tearing which can lead to hard-to-debug user bugs.

yes i understand. but please consider advanced users. i try to explain better here https://github.com/facebookexperimental/Recoil/issues/829

Read more comments on GitHub >

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

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