In JavaScript, what is created every time a function is declared, preventing variables inside from being visible outside?
A local scope.
What JavaScript mechanism allows an inner function to access and ‘remember’ variables from its containing (outer) function’s scope?
A closure.
When a closure is formed, it is essentially a _____ of all the ‘outside’ data frozen in time.
snapshot
In React, every single callback function declared inside a component is a _____.
closure
Besides callbacks, what two common React hooks also create closures with the functions passed to them?
The useEffect and useCallback hooks.
What is the term for a closure that captures and holds onto outdated values of state, props, or other variables?
A stale closure.
A closure lives for as long as a _____ to the function that created it exists.
reference
In React, what is the primary mechanism for ‘refreshing’ a cached closure created by useCallback with the latest state or props?
The dependencies array.
What happens if you use a state variable inside a useCallback but forget to add that state variable to the dependencies array?
The callback will form a stale closure, always accessing the initial value of the state.
Why does storing a state-dependent function in a useRef on initial render often lead to a stale closure?
Because the ref is initialized only once, and the closure it holds is never refreshed with subsequent state changes.
To fix a stale closure caused by a function stored in a ref, what must you do whenever the dependencies (state/props) change?
You must manually update ref.current with a new function (closure) that captures the latest data.
In the initial React.memo example, what is the dilemma when trying to memoize a HeavyComponent that needs a state-dependent onClick handler?
Wrapping onClick in useCallback with the state as a dependency causes it to be re-created on every state change, defeating React.memo.
When using React.memo with a custom comparison function that only compares a title prop, what problem occurs for an onClick prop that depends on state?
The component never re-renders, so it holds a reference to the very first onClick closure, which becomes stale.
To escape the stale closure trap for memoized components, what React hook is used to hold a mutable reference to the latest version of a function?
The useRef hook.
In the useRef pattern to avoid stale closures, which hook is used to update ref.current with the latest function on every re-render?
The useEffect hook.
In the useRef pattern, why is the useEffect that updates ref.current written without a dependencies array?
To ensure it runs on every re-render, thus always updating the ref with a fresh closure capturing the latest state.
In the useRef pattern, what kind of function is actually passed as a prop to the memoized component?
A stable function created with useCallback and an empty dependency array.
In the useRef pattern, what does the stable useCallback function do to access the latest state?
It calls the function currently stored in ref.current().
Why isn’t the ref object itself needed in the dependency array of the useCallback in the stale closure escape pattern?
Because the ref object returned by useRef is stable and its identity never changes between re-renders.
How does the useRef pattern solve the memoization dilemma?
It provides a stable function prop (onClick) so React.memo works, while that function can internally access the latest state via the mutable ref.
Consider the code: const something = (value) => { return () => console.log(value); }; const first = something('one'); first(); What is logged?
The string ‘one’ is logged, because a closure captured the value variable.
If a function inside is created within a function something, can something access variables declared only within inside?
No, variables declared in an inner scope are not visible to the outer scope.
What is the consequence of caching a closure and then returning that cached version on subsequent calls with different arguments?
It creates a stale closure, where the function always executes with the data from when it was first created and cached.
What built-in React hook behaves similarly to manually caching a function and refreshing it only when specific values change?
The useCallback hook.