10. Closure in React Flashcards

(27 cards)

1
Q

In JavaScript, what is created every time a function is declared, preventing variables inside from being visible outside?

A

A local scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What JavaScript mechanism allows an inner function to access and ‘remember’ variables from its containing (outer) function’s scope?

A

A closure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When a closure is formed, it is essentially a _____ of all the ‘outside’ data frozen in time.

A

snapshot

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In React, every single callback function declared inside a component is a _____.

A

closure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Besides callbacks, what two common React hooks also create closures with the functions passed to them?

A

The useEffect and useCallback hooks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the term for a closure that captures and holds onto outdated values of state, props, or other variables?

A

A stale closure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A closure lives for as long as a _____ to the function that created it exists.

A

reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In React, what is the primary mechanism for ‘refreshing’ a cached closure created by useCallback with the latest state or props?

A

The dependencies array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you use a state variable inside a useCallback but forget to add that state variable to the dependencies array?

A

The callback will form a stale closure, always accessing the initial value of the state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why does storing a state-dependent function in a useRef on initial render often lead to a stale closure?

A

Because the ref is initialized only once, and the closure it holds is never refreshed with subsequent state changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

To fix a stale closure caused by a function stored in a ref, what must you do whenever the dependencies (state/props) change?

A

You must manually update ref.current with a new function (closure) that captures the latest data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the initial React.memo example, what is the dilemma when trying to memoize a HeavyComponent that needs a state-dependent onClick handler?

A

Wrapping onClick in useCallback with the state as a dependency causes it to be re-created on every state change, defeating React.memo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A

The component never re-renders, so it holds a reference to the very first onClick closure, which becomes stale.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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?

A

The useRef hook.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In the useRef pattern to avoid stale closures, which hook is used to update ref.current with the latest function on every re-render?

A

The useEffect hook.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In the useRef pattern, why is the useEffect that updates ref.current written without a dependencies array?

A

To ensure it runs on every re-render, thus always updating the ref with a fresh closure capturing the latest state.

17
Q

In the useRef pattern, what kind of function is actually passed as a prop to the memoized component?

A

A stable function created with useCallback and an empty dependency array.

18
Q

In the useRef pattern, what does the stable useCallback function do to access the latest state?

A

It calls the function currently stored in ref.current().

19
Q

Why isn’t the ref object itself needed in the dependency array of the useCallback in the stale closure escape pattern?

A

Because the ref object returned by useRef is stable and its identity never changes between re-renders.

20
Q

How does the useRef pattern solve the memoization dilemma?

A

It provides a stable function prop (onClick) so React.memo works, while that function can internally access the latest state via the mutable ref.

21
Q

Consider the code: const something = (value) => { return () => console.log(value); }; const first = something('one'); first(); What is logged?

A

The string ‘one’ is logged, because a closure captured the value variable.

22
Q

If a function inside is created within a function something, can something access variables declared only within inside?

A

No, variables declared in an inner scope are not visible to the outer scope.

23
Q

What is the consequence of caching a closure and then returning that cached version on subsequent calls with different arguments?

A

It creates a stale closure, where the function always executes with the data from when it was first created and cached.

24
Q

What built-in React hook behaves similarly to manually caching a function and refreshing it only when specific values change?

A

The useCallback hook.

25
A `useRef` hook, when initialized with a function `() => { console.log(state); }`, will always log the _____ value of the state if `ref.current` is not updated.
initial
26
When a closure 'freezes' its surrounding data, does it make objects within its scope immutable?
No, it only freezes the reference to the object; the object itself remains mutable.
27
In the `Form` example, using `React.memo` with `(before, after) => before.title === after.title` causes the `HeavyComponent` to hold onto the _____ `onClick` function it received.
very first