facebookexperimental/Recoil

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

オープン

#2,058 opened on 2022/10/06

 (2 件のコメント) (0 件のリアクション) (0 人の担当者)JavaScript (1,151 件のフォーク)batch import
bughelp wanted

Repository metrics

Stars
 (19,428 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

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 ?

コントリビューターガイド