6. React Reconciliation Flashcards

(35 cards)

1
Q

What function is the HTML-like syntax (JSX) in React transformed into?

A

It is transformed into the React.createElement function.

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

What does the React.createElement function return?

A

It returns a description object, also known as a React Element.

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

In a React Element object, what does the type property point to?

A

It points to a component, a memoized component, or a string representing an HTML tag.

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

When will React re-render an element if its type remains the same between renders?

A

It will re-render if the component referenced in type is not memoized with React.memo.

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

In a conditional render like {isCompany ? <Input /> : <TextPlaceholder />} what happens to the components when isCompany flips from false to true?

A

The TextPlaceholder component is unmounted, and the Input component is mounted.

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

In a conditional render {condition ? <Input /> : <Input />} with different props, why is the internal state of the Input preserved when the condition changes?

A

Because the component type (Input) is the same, React re-renders the existing component with new props instead of unmounting and mounting a new one.

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

React avoids direct DOM manipulation by creating and modifying a tree of objects commonly known as the _____.

A

Virtual DOM

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

In React’s Virtual DOM, what is the value of the type property for a standard HTML element like <input>?

A

The value is a string with the HTML tag name, e.g., ‘input’.

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

In React’s Virtual DOM, what is the value of the type property for a React component like <MyComponent />?

A

The value is a reference to the component’s function itself (e.g., MyComponent).

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

During an initial render, what does React do when it encounters an element whose type is a function?

A

It calls the function (the component) and then iterates over the tree that the function returned.

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

During a re-render, what is the primary property React compares between the ‘before’ and ‘after’ element objects to decide its action?

A

React compares the type property.

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

If an element’s type changes between re-renders, what two actions will React perform?

A

React will unmount the ‘previous’ component and mount the ‘next’ component.

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

What is the primary reason it’s considered an anti-pattern to define a component inside another component’s render body?

A

The inner component function is recreated on every re-render of the parent, resulting in a different function reference each time.

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

What is the consequence for reconciliation when a component’s type is a different function reference on every render?

A

React sees the type as having changed, causing it to unmount the old component and mount a new one on every single re-render.

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

The unintentional process of a component being destroyed and recreated on every re-render is called _____.

A

re-mounting

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

What are two negative consequences of unintentional re-mounting?

A

It is terrible for performance and causes the loss of all associated state and DOM elements (like focus).

17
Q

In the ‘mysterious bug’ example, why did {isCompany ? <Input /> : <Input />} preserve state?

A

Because the type property for both conditional branches was a reference to the exact same Input component function.

18
Q

When React encounters an array of children during reconciliation, how does it compare the elements between renders by default?

A

It compares elements based on their position (index) in the array.

19
Q

How can you fix the ‘mysterious bug’ where two different Input components share state by altering their position in the render output?

A

By structuring the conditional render so the components occupy different positions, like {isCompany ? <Input /> : null}{!isCompany ? <Input /> : null}.

20
Q

What is the primary purpose of the key attribute in React?

A

It provides a stable and unique identity for an element within an array of children across re-renders.

21
Q

When rendering a dynamic list, if the key and type of an element match between renders, what will React do?

A

React will re-use the existing component instance, with all its associated state and DOM, regardless of its position in the array.

22
Q

If you reorder a list of stateful components that do not have keys, what observable bug might occur?

A

The component’s internal state will remain with its original position (index) rather than moving with the data item it represents.

23
Q

True or False: The key attribute is primarily a performance optimization that prevents list items from re-rendering.

A

False; key is for identity and instance re-use, while React.memo is used to prevent re-renders.

24
Q

To prevent re-renders of items in a list, you should wrap the item component in _____.

25
When rendering a dynamic list of memoized components that can be reordered, why is using the array `index` as a `key` a bad practice?
The component at a given index will receive new props from different data, causing a re-render and defeating the purpose of memoization.
26
What should be used as a `key` for a dynamic list of memoized components to ensure optimal performance when reordering?
A stable and unique identifier from the data item itself, such as a database ID.
27
What is the 'state reset' technique in React?
Forcing a component to unmount and mount from scratch by changing its `key` attribute between re-renders.
28
How could you reset the state of an `` component whenever the page URL changes?
By passing the URL as the `key` prop to the Input, like ``.
29
What is the performance consideration when using the 'state reset' technique with a changing `key`?
It forces a complete unmount and mount cycle, which can be expensive for large or complex components.
30
How can you use the `key` attribute to force React to re-use a component instance even if it appears in a different position in the children array?
By giving the components in different positions the exact same `key` value.
31
Why does React mandate `key`s for dynamic lists (e.g., from `.map()`) but not for a static list of sibling elements?
React assumes static elements won't change order, so their position is a reliable identifier, whereas dynamic lists may be reordered, requiring an explicit key.
32
When a dynamic array (from `.map()`) and a static element are rendered as siblings, how does React structure them in the virtual DOM's children array?
React places the entire dynamic array as a single item (an array) in the first position, followed by the static element.
33
If you add an item to a dynamic array that is followed by a static component, will the static component re-mount?
No, because React treats the entire dynamic list as a single child, so the static component's position in the parent's children array remains stable.
34
During reconciliation, React will re-render an element if its _____ and its position in the array are the same between renders.
type