Câu hỏi phỏng vấn Reactjs
Câu hỏi

How does React work?

Câu trả lời

React is a popular JavaScript library used for building user interfaces, particularly for single-page applications where a responsive and dynamic interaction is needed. At the core of React's efficiency and performance are several key concepts and mechanisms, including the Virtual DOM, components, and the reconciliation process.

Virtual DOM

React uses a concept known as the Virtual DOM, which is a lightweight copy of the actual DOM (Document Object Model). The Virtual DOM is an in-memory representation that allows React to keep track of changes in the UI in an efficient manner[1][4][5]. When changes occur in the application's state or props, React updates this Virtual DOM first. This approach minimizes direct interactions with the actual DOM, which are costly in terms of performance.

Components

React applications are built using components, which are reusable and independent pieces of code that describe a part of the user interface[7][10]. Each component has its own state and props:

  • State refers to data that changes over time within the component.
  • Props (short for "properties") are used to pass data and event handlers down to child components.

Components can be either class-based or functional, and React 16.8 introduced Hooks, which allow functional components to handle state and side effects similar to class components[9].

Reconciliation and Diffing Algorithm

When the state of a component changes, React needs to determine what changes need to be made to the DOM. This process is known as reconciliation[2][3][6][18]. React uses a diffing algorithm to compare the new Virtual DOM tree with the previous one and computes the minimal set of changes required to update the actual DOM.

  1. Diffing: React compares the two versions of the Virtual DOM and identifies differences[17]. It updates only the parts of the DOM that have actually changed, rather than re-rendering everything. This selective update process significantly enhances performance.

  2. Batch Updates: React batches multiple DOM updates into a single re-render for better performance. This means that the actual DOM is updated in a way that minimizes the performance impact[2][3].

  3. Keys: React uses keys to identify elements in...

entry

entry

Gợi ý câu hỏi phỏng vấn

senior

What is the purpose of super(props) ?

middle

What's the difference between useRef and createRef ?

senior

How would you store non-state/instance variables in functional React components?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào