facebookexperimental/Recoil

consider adding `useRecoilMemo`

开放

#985 创建于 2021年4月23日

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

仓库指标

星标
 (19,428 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

I find myself in a situation often where a component needs to build up some derived state from a series of different atoms/selectors—this may even be looping over an array of atoms/selectors. And to do that I'm forced to create a new selector that represents this components derived state (to have access to get in a context where looping is fine).

Instead, it would be nice if there was a useRecoilMemo which was similar to useMemo except it received ({ get }) as a parameter. So you could do complex aggregations right inline without having to define these one-off atoms/selectors.

Here's a very simplified example:

const Component = ({ users }) => {
  const data = useRecoilMemo(({ get }) => {
    return users.map(user => get(userProfileSelectorFamily(user.id)))
  }, [users])
  
  return <div>{data...}</div>
}

But these aggregations are often much more complex.

贡献者指南