0

If I call a useCallback method to update a redux store value that is in its dependency array, will I get the updated value immediately or on the next call of the method?

For example, if the value in this example is 5 when I call the method, will I log 5 or 10?

const doSomething = React.useCallback(async () => {
    await dispatch(changeValue(10));
    console.log(value);     
}, [value]);

I think that there is a chance it could be if the update is fast enough, but I would like to know what is the expected behavior.

Ryan Pergent
  • 4,432
  • 3
  • 36
  • 78
  • 1
    `useCallback` returns a memoized version of a function in which the local scope `value = 5`. Even if the update was fast (although this is not important, because the value will change only in the next render cycle) you will always get 5 in cosnole. – Nikita Madeev Jun 29 '20 at 10:07
  • Thank you :). If you make it an answer, I can accept it. – Ryan Pergent Jun 29 '20 at 10:10

0 Answers0