facebookexperimental/Recoil

consider adding `useRecoilMemo`

Open

#985 建立於 2021年4月23日

在 GitHub 查看
 (2 留言) (10 反應) (0 負責人)JavaScript (19,428 star) (1,151 fork)batch import
enhancementhelp wanted

描述

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.

貢獻者指南