facebookexperimental/Recoil

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

Open

#2.058 geöffnet am 6. Okt. 2022

Auf GitHub ansehen
 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)JavaScript (19.428 Stars) (1.151 Forks)batch import
bughelp wanted

Beschreibung

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