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

What are the different phases of ReactJS component lifecycle?

Câu trả lời

In ReactJS, the lifecycle of a component is a series of events that happen from the moment the component is first conceived until it is destroyed and removed from the DOM. The lifecycle is divided into distinct phases, each with its own set of lifecycle methods that allow developers to hook into the events and execute code at specific times. The different phases of the ReactJS component lifecycle are:

Initializing Phase

This is the initial phase of the React component lifecycle. It involves setting up the initial state and default props of the component. The methods involved in this phase are:

  • getDefaultProps(): This method is invoked before a component is created to define default props.
  • getInitialState(): This method is invoked before a component is created to define the initial state[2].

Mounting Phase

The mounting phase is when the component is being inserted into the DOM. It consists of the following methods:

  • componentWillMount(): This method is called just before the component is mounted onto the DOM.
  • render(): This is the method where the component returns the elements it wants to render.
  • componentDidMount(): This method is called immediately after the component is mounted onto the DOM[2][3].

Updating Phase

The updating phase occurs when the component's state or props change, triggering a re-render. The methods in this phase include:

  • componentWillReceiveProps(): This method is invoked before a mounted component receives new props.
  • shouldComponentUpdate(): This method determines whether the component should update.
  • componentWillUpdate(): This method is called just before the component is about to update.
  • render(): Again, the component ...
middle

middle

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

senior

When to use useState vs useReducer ?

junior

When rendering a list what is a key and what is it's purpose?

senior

Why would you need to bind event handlers to this ?

Bình luận

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

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