facebookexperimental/Recoil

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

Ouverte

#2 058 ouverte le 6 oct. 2022

 (2 commentaires) (0 réaction) (0 personne assignée)JavaScript (1 151 forks)batch import
bughelp wanted

Métriques du dépôt

Stars
 (19 428 étoiles)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

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 ?

Guide contributeur