facebookexperimental/Recoil

consider adding `useRecoilMemo`

Open

#985 opened on Apr 23, 2021

View on GitHub
 (2 comments) (10 reactions) (0 assignees)JavaScript (1,151 forks)batch import
enhancementhelp wanted

Repository metrics

Stars
 (19,428 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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.

Contributor guide