facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.5k stars 1.18k forks source link

Feature request: Selector-like hook for components #2303

Open derolf opened 5 months ago

derolf commented 5 months ago

Imagine we had

const [get, getCallback] = useRecoil();

where get and getCallback have the same semantics as in the getter of selector.

Then I could write:

function Foo() {
  const [get, getCallback] = useRecoil();
  return <>{get(myAtom)} <button onClick={() => getCallback(({i}) => i.set(myAtom, 42))}>42</button></>
} 
suren-atoyan commented 5 months ago

what was the motivation behind this?

derolf commented 5 months ago

The motivation is to avoid value/state-hooks for every piece the component is using.