8. React Context and Performance Flashcards

(27 cards)

1
Q

What is a common, often negative, reputation of React Context regarding performance?

A

Context has a reputation for causing spontaneous and unstoppable re-renders, leading some developers to avoid it.

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

Contrary to its reputation, how can React Context positively impact performance when used correctly?

A

It can prevent unnecessary re-renders of intermediate components and significantly improve app performance.

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

What problem does lifting state to a common ancestor component to share it between distant children introduce, besides performance issues?

A

It leads to ‘prop drilling,’ where intermediate components receive and pass down props they don’t use, bloating their API.

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

In the ‘prop drilling’ example, why does clicking the ExpandButton cause VerySlowComponent inside MainPart to re-render unnecessarily?

A

Because the state is held in the parent Page component, and a state update there causes its entire component tree, including MainPart and its children, to re-render.

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

How does React Context solve the problem of ‘prop drilling’?

A

It allows passing data directly from a top-level provider component to a nested consumer component, bypassing all intermediate components.

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

When using Context with the ‘children as props’ pattern, why are intermediate components like Layout and Sidebar not re-rendered when the context’s state changes?

A

Because children are just props, and components do not re-render just because their parent’s state changes; only the consuming components are affected.

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

What hook is used to access the value provided by a React Context?

A

The useContext hook is used to access the context’s value.

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

When state inside a Context provider changes, which components in the application will re-render as a result?

A

Only the components that consume that specific context using the useContext hook will re-render.

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

What is the primary rule regarding re-renders for components that consume a React Context?

A

Context consumers will re-render every time the value prop on the Provider changes.

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

Why is it problematic if a component consuming a part of a context value re-renders when a different, unused part of that value changes?

A

It causes unnecessary re-renders, as the component re-renders even though the data it actually depends on has not changed.

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

Can React.memo or other standard memoization techniques easily prevent a context consumer from re-rendering when the context value changes?

A

No, these re-renders cannot be easily prevented with standard memoization techniques once the context value has changed.

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

What problem occurs if the value object passed to a Context Provider is re-created on every render of the provider component?

A

This causes a referential equality issue; the value is a new object every time, triggering a re-render in all context consumers even if the underlying data is the same.

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

In the example where a scroll state is added to the Layout component, why do all NavigationController context consumers re-render on every scroll event?

A

The scroll state change causes Layout to re-render, which in turn causes its child NavigationController to re-render, creating a new value object for the provider and triggering all consumers.

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

How can you prevent unintentional re-renders of context consumers caused by the provider’s parent re-rendering?

A

By memoizing the value passed to the provider using the useMemo and useCallback hooks.

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

Why is always memoizing the context provider’s value prop considered a good practice rather than premature optimization?

A

It prevents potentially large and hard-to-debug performance problems that can be introduced later if a parent component starts re-rendering.

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

A component uses only the open function from a context. Why will it still re-render when the isNavExpanded state (which it doesn’t use) changes?

A

Because the entire value object on the provider changes, and all components that consume the context will re-render, regardless of which part of the value they use.

17
Q

What optimization technique involves creating multiple contexts to separate volatile state from stable functions?

A

Splitting providers.

18
Q

In the ‘splitting providers’ pattern, one context would hold the changing state (data), while the other would hold _____.

A

the stable API functions that do not change

19
Q

When splitting providers, why can’t a toggle function that depends on the current state be placed in the ‘stable API’ context?

A

Because its dependency on the state means the function would need to be re-created whenever the state changes, making the API context value unstable.

20
Q

What React hook is recommended over useState to solve the issue of state-dependent functions in a split provider pattern?

21
Q

In the useReducer pattern, what is the role of the dispatch function?

A

It allows components to send named ‘actions’ to the reducer to trigger state updates without needing to know the current state.

22
Q

Why does using useReducer allow functions like toggle to be placed in a stable API context provider?

A

Because the dispatch function returned by useReducer has a stable identity, and API functions that only call dispatch do not depend on the state itself, so they also remain stable.

23
Q

The reducer function accepts two arguments: the current state and the _____.

24
Q

If you don’t want to refactor to use split providers or reducers, what trick can mimic context selectors to prevent re-renders?

A

Using a Higher-Order Component (HOC) combined with React.memo.

25
In the HOC selector pattern, where is `useContext` called to get the context value?
It is called inside the component that is returned by the HOC.
26
What is the final, crucial step in the HOC selector pattern that actually prevents the wrapped component from re-rendering?
Wrapping the component passed into the HOC with `React.memo` before rendering it.
27
How does the HOC selector pattern work to prevent re-renders?
The HOC's wrapper component re-renders on context change, but the inner, memoized component does not because its props (including the stable function from the context) have not changed.