I am trying to understand more about the dependency requirements when using a React hook. Using useMemo as an example, I can increment the counter and the value of dog is rendered as the same value as count. This is odd to me because foo is not referenced as a dependency and from my understanding, this closure should be stale.
From the React docs in regards to useMemo:
every value referenced inside the function should also appear in the dependencies array
It doesn't seem like this^ is a requirement. If someone could help me better understand how the closure works in this particular case, I'd appreciate it.
function App() {
const [count, setCount] = useState(0);
const foo = useMemo(() => count, [count]);
const dog = useMemo(() => foo, [count]); // incrementing count updates this value
return (
<div className="App">
{dog}
<h1>Hello CodeSandbox</h1>
<h2>You clicked {count} times!</h2>
<button onClick={() => setCount(count - 1)}>Decrement</button>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}