facebookexperimental/Recoil

`set()` does not work inside `getCallback()` from non-async selector `get()`

Open

#2,058 opened on Oct 6, 2022

View on GitHub
 (2 comments) (0 reactions) (0 assignees)JavaScript (1,151 forks)batch import
bughelp wanted

Repository metrics

Stars
 (19,428 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I'm trying to generate a callback that is able to update an atom, I attempted to use getCallback and use the set function it provides, but it does nothing:


const tokenState = atom({
  key: 'token',
  default: 'no token yet',
});

const auth = selector({
  key: 'auth',
  get: async ({ get, getCallback }) => {
    const login = getCallback(({ set }) => async () => {
      console.log('logging in...');
      set(tokenState, 'logged in!');
    });

    return {
      token: get(tokenState),
      login,
    };
  },
});

Expected behaviour:

  • Either an error is thrown telling the user this is not how it works
  • Or the atom is updated as one would expect

Example: https://stackblitz.com/edit/react-ts-4ezf3k?file=App.tsx

Update: Changing the get of the selector to a non async function appears to make it work as expected, any insight ?

Contributor guide