yuchi / hooks.macro

☔️ Babel Macros for automatic React Hooks memoization invalidation
MIT License
357 stars 5 forks source link

Extract indirect dependencies #1

Closed yuchi closed 5 years ago

yuchi commented 5 years ago

Simple example provided by @threepointone on Twitter:

function App(){
  let [val, setVal] = useState(0);

  let memoed = useAutoMemo(() => someLocallyScopedFunc());

  function someLocallyScopedFunc() {
    return val * 2;
  }
}

We currently get:

function App(){
  let [val, setVal] = useState(0);

  let memoed = useMemo(
    () => someLocallyScopedFunc(),
    [someLocallyScopedFunc]
    // we would want [val] here!
  );

  function someLocallyScopedFunc()() {
    return val * 2;
  }
}