Hacker News new | ask | show | jobs
by ng12 2247 days ago
But everything in that dependency array is from lexical scope, correct? Your hooks execute in the scope of that call to MyComponent().
1 comments

Correct, but the callback passed to useEffect is only scoped to the call stack triggered by the dependency array. So, this makes the callback passed to useEffect dynamically scoped.
The callback is always defined it's just not always invoked. Otherwise it's a regular lexical closure like any callback in JavaScript. Maybe I'm not understanding what you mean by dynamic scope.
Yes, it's just regular lexical closure. I don't think his comment was literal since JS itself is lexically scoped. If you think about how `this` works in JS, it's very similar to dynamic scoping because it matters where the function is called.

With hooks and dependency arrays, similar to dynamically scoped languages, it matters where the function is called.

Thing is, people rarely write functions and use `this` is in dynamically scoped ways anymore. Remember when you had to explicitly bind function scope everywhere? With ES6 and arrow functions, I don't miss that.

Anyway, hooks forces you into that mindset now.