facebookexperimental/Recoil

consider adding `useRecoilMemo`

Ouverte

#985 ouverte le 23 avr. 2021

 (2 commentaires) (10 réactions) (0 personne assignée)JavaScript (1 151 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (19 428 étoiles)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

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.

Guide contributeur