React behind the scenes Flashcards

1
Q

What is the purpose of the render phase in React?

A

The render phase prepares a list of updates to be made to the DOM.

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

How does the commit phase differ from the render phase in terms of synchronization?

A

The commit phase is synchronous, while the render phase can be paused and resumed.

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

What is the primary responsibility of the commit phase in React?

A

The commit phase writes updates to the DOM, making changes visible to the user.

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

What happens to the work in progress fiber tree after the commit phase?

A

The work in progress fiber tree becomes the current tree for the next render cycle.

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

Which library is responsible for writing updates to the DOM in React?

A

React DOM is the library responsible for the commit phase and interacting with the DOM.

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

Why is React designed to be independent from the platform where elements will be shown?

A

React can be used with various hosts, including web browsers, native mobile apps, and more, making it versatile.

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

What is the purpose of the reconciliation process, and what does it result in?

A

Reconciliation minimizes DOM updates by finding the smallest number of updates needed to reflect the latest state changes.

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

Is the render phase synchronous or asynchronous, and why?

A

The render phase is asynchronous to allow for concurrent features and prevent the JavaScript engine from being blocked.

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

How does the commit phase ensure a consistent user interface?

A

The commit phase is synchronous, performing all DOM updates in one go.

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

What triggers the visual update of the user interface on the screen after the commit phase?

A

The browser repaints the screen to make the DOM updates visible to the user.

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

What is the conceptual difference between React components, component instances, and React elements?

A

Components are like blueprints for the user interface, component instances are physical manifestations of components with their own state and lifecycle, and React elements are JavaScript objects that represent the result of using a component in code.

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

How can you describe a React component in terms of its purpose?

A

A React component is like a blueprint or template for describing a piece of the user interface.

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

What is the primary function of a React component?

A

A React component is a JavaScript function that returns React elements, describing the structure of the UI.

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

How does React create component instances from components?

A

React creates component instances each time the component is used in code, by calling the component function.

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

What defines the lifecycle of a React component instance?

A

Each component instance has its own lifecycle, including birth, existence, and eventual termination.

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

In practice, how are the terms “component” and “component instance” often used interchangeably?

A

While technically different, “component” and “component instance” are often used interchangeably in common usage.

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

What is a React element, and how is it created?

A

A React element is a big immutable JavaScript object created by React when calling component functions. It represents the UI structure.

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

What information does a React element contain?

A

A React element contains all the information required to create DOM elements for the component instance.

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

How are React elements related to DOM elements in the rendering process?

A

React elements are converted to DOM elements and rendered onto the screen by the browser.

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

How can you view a component instance in your code?

A

You can log a component instance to the console by directly using the component and passing it to the console.log() function.

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

What is the output when you log a component instance to the console?

A

Logging a component instance displays a React element representing the component, with its type and optional props.

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

Why does React use the symbol “$$typeof” in React elements?

A

React uses the “$$typeof” symbol as a security feature to protect against cross-site scripting attacks, ensuring that symbols cannot be transmitted via JSON.

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

Is it possible to call React components directly as functions?

A

Yes, it is possible to call React components directly, but it results in a different output compared to using JSX.

24
Q

What is the output when React components are called directly as functions?

A

When called directly, React components return a raw React element, not recognized as a component instance.

25
Q

What are the consequences of calling components directly instead of rendering them in JSX?

A

Calling components directly can lead to issues such as violating the rules of hooks and incorrect state management.

26
Q

How does calling a component directly affect state management?

A

Calling a component directly may result in the component sharing its state with a parent component rather than managing its own state.

27
Q

What is the recommended best practice for using React components?

A

Always render components in JSX, following the blueprint pattern, to ensure proper recognition as component instances with their own state and props.

28
Q

What is the distinction between a component instance and a component blueprint?

A

A component instance is a physical manifestation with its own state and props, while a component is the blueprint or template for creating instances.

29
Q

What are the main phases involved in rendering components with React?

A

The main phases of React rendering are the Render Phase and the Commit Phase, followed by the browser repaint phase.

30
Q

What happens during the Render Phase in React?

A

In the Render Phase, React calls component functions and determines how to update the DOM to reflect the latest state changes. However, it does not actually update the DOM during this phase.

31
Q

What is the purpose of the Commit Phase in React rendering?

A

The Commit Phase is responsible for updating the DOM based on the changes determined in the Render Phase. It inserts, deletes, and updates DOM elements to reflect the current state of the application.

32
Q

What occurs during the browser repaint phase in the React rendering process?

A

In the browser repaint phase, the browser notices that the DOM has been updated, and it repaints the screen to visually reflect the changes made by React.

33
Q

What are the two ways in which a render can be triggered in a React application?

A

A render can be triggered by the initial application load (initial render) or by a state update occurring in one or more component instances (re-render).

34
Q

Does a render in React only affect the component where a state update occurred?

A

No, a render in React is triggered for the entire application, not just a single component. React analyzes the entire component tree to determine what needs to be updated in the DOM.

35
Q

Is a render triggered immediately after a state update in React?

A

A render is not necessarily triggered immediately after a state update. It is scheduled for when the JavaScript engine has some free time, but the delay is usually minimal and imperceptible to users.

36
Q

Are all renders in React processed individually?

A

In some cases, like when multiple setState calls occur in the same function, renders are batched together for efficiency, as React optimizes the rendering process.

37
Q

hat is the Virtual DOM in React?

A

The Virtual DOM is a concept in React where the entire component tree is represented as a JavaScript object tree (React element tree). It is used to efficiently track and manage updates to the DOM.

38
Q

What is reconciliation in React?

A

Reconciliation is the process in React where the virtual DOM is compared to the current Fiber tree to determine what changes are needed in the DOM to reflect the latest state changes.

39
Q

What is the Fiber reconciler in React?

A

The Fiber reconciler is the internal engine of React responsible for the rendering process. It manages the Fiber tree, which is used to track component instances, state, and the work to be done during rendering.

40
Q

What is the Fiber tree in React?

A

The Fiber tree is an internal representation of the component tree in React. It consists of Fiber nodes for each component instance and is used to efficiently track and manage updates during rendering.

41
Q

Is the Fiber tree in React immutable?

A

No, the Fiber tree is mutable. It is created during the initial render and then updated and mutated in future reconciliation steps, making it a suitable place to store component state, props, and work to be done.

42
Q

What is the advantage of asynchronous rendering in React?

A

Asynchronous rendering allows React to split rendering into chunks, prioritize tasks, pause and resume rendering, and implement modern features like Suspense and transitions. It helps improve performance in large applications.

43
Q

What is diffing in the context of React reconciliation?

A

Diffing is the process of comparing elements step-by-step in the Fiber tree to determine what changes are needed between the current Fiber tree and the updated Fiber tree based on the new virtual DOM. It is a key part of the reconciliation process.

44
Q

What is the purpose of the “list of effects” in React’s rendering process?

A

The “list of effects” contains a record of DOM operations (mutations) that need to be performed in the commit phase to update the actual DOM to reflect the latest state changes. It is generated during the reconciliation process.

45
Q

What are the key phases involved in rendering a React application?

A

1) Trigger: It starts with a trigger, which can be an initial render or a state update in a component.
2) Render Phase: React calls component functions to create updated React elements, which form a virtual DOM.
3) Reconciliation: The new virtual DOM is reconciled with the current Fiber tree to identify necessary DOM updates.
4) Commit Phase: React applies the list of DOM updates to the actual DOM in a synchronous manner.
5) Browser Paint: The browser repaints the screen to reflect the updated DOM.
6) Render Cycle: The work in progress Fiber tree becomes the current tree for the next render cycle.

46
Q

What are the two fundamental assumptions underlying the diffing algorithm in React reconciliation?

A

The diffing algorithm in React reconciliation is based on two fundamental assumptions:
1. Elements of different types will produce different trees.
2. Elements with a stable key will stay the same across renders.

47
Q

How does React handle elements in the tree that have changed, such as their type?

A

When an element in the tree has changed (e.g., its type), React assumes that the element itself and all its children are no longer valid. These elements are destroyed and removed from the DOM, including their state. New elements are then created in their place.

48
Q

What happens when the same element is present at the same position in the tree between two renders?

A

If the same element is present at the same position in the tree between two renders, React preserves it in the DOM, including all child elements and component state. This element is not removed or destroyed.

49
Q

How does React handle changes to attributes (for DOM elements) and props (for React elements) without replacing the entire element?

A

React efficiently mutates the attributes of DOM elements and passes new props to React elements when attribute or prop changes occur. The elements themselves are not removed from the DOM, and state is preserved.

50
Q

How can developers control whether React creates a new component instance with new state or updates an existing one when elements are changed?

A

Developers can use the key prop to control the behavior. When elements have stable keys, React updates them in place. When keys change, React creates new elements and instances, effectively resetting state.

51
Q

What is the key prop used for in React?

A

The key prop in React is used to uniquely identify elements, both DOM elements and React elements, and it serves two main purposes: 1) to optimize rendering performance by informing React about element stability in the tree, and 2) to reset state in component instances when the key changes.

52
Q

How does using stable keys in React elements optimize rendering performance?

A

When stable keys (keys that remain the same across renders) are used in React elements, React will keep those elements in the DOM even if their position in the tree changes. This optimization avoids unnecessary removal and recreation of DOM elements, resulting in improved rendering performance, especially in large lists.

53
Q

How can the key prop be used to reset the state of a component instance in React?

A

By changing the key prop of a component instance between renders, React treats it as a different instance and creates a new DOM element for it. This process resets the state of the component instance, allowing developers to manage state changes effectively.

54
Q

In practical terms, when should you use a stable key, and when should you use a changing key in React?

A

Use a stable key when you want to optimize rendering performance by keeping the same element in the DOM even if its position changes. Use a changing key when you need to reset the state of a component instance, ensuring that it gets recreated with a fresh state.

55
Q
A