facebookexperimental/Recoil

useRecoilCallback batched set calls update state incorrectly

Open

#1935 opened on Aug 5, 2022

View on GitHub
 (14 comments) (0 reactions) (0 assignees)JavaScript (19,428 stars) (1,151 forks)batch import
bughelp wanted

Description

We've been using Recoil quite extensively and believe we have encountered a bug with useRecoilCallback and state updating.

There is a rootState atom that we want to update via a useRecoilCallback that contains multiple set calls. If the set calls within useRecoilCallback try to set the recoil state of rootState directly or through a single selector layer, the final rootState is as expected. However, if there is more than one layer of selector dependencies, it is only the last set that has a result on rootState.

Here is the CodeSandbox.

Expected: All steps change their state from ['1', '2', '3'] to ['one', 'two', 'three'] Result: The only step changing the state is the last one for which set has run. In effect: ['1', '2', '3'] changes to ['1', '2', 'three']

Contributor guide