Skip to content

useState hook value is stale inside of onSnapshot #1920

Answered by EmilTholin
kwhitaker asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @kwhitaker!

The easiest solution might be to make the computed value into a view and it will be kept in sync automatically:

const Store = types
  .model("Store", {
    arr: types.array(types.array(types.array(types.number)))
  })
  .views((self) => ({
    get computation() {
      return doSomeCompute(self.arr);
    }
  }))
  .actions((self) => ({
    setArr(next) {
      self.arr = next;
    }
  }));

// ...

const ObservedComponent = observer(({ computation }) => {
  return <SomeBasicComponentThatNeedsTheStateValue {...computation} />;
});

If you absolutely don't want to pollute your MST model with this computation you could use the useLocalObservable hook and derive the computed ("v…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@kwhitaker
Comment options

@EmilTholin
Comment options

@kwhitaker
Comment options

Answer selected by kwhitaker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants