facebookexperimental/Recoil

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

开放

#2,058 创建于 2022年10月6日

 (2 条评论) (0 个反应) (0 位负责人)JavaScript (1,151 个派生)batch import
bughelp wanted

仓库指标

星标
 (19,428 个星标)
PR 合并指标
 (30 天内没有已合并 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 ?

贡献者指南