facebookexperimental/Recoil

consider adding `useRecoilMemo`

Aberta

#985 aberto em 23 de abr. de 2021

 (2 comentários) (10 reações) (0 responsável)JavaScript (1.151 forks)batch import
enhancementhelp wanted

Métricas do repositório

Stars
 (19.428 estrelas)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 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.

Guia do colaborador