Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between componentWillMount and componentDidMount in ReactJS?

Tags:

reactjs

I looked at Facebook's documentation at (React.Component) and it mentions how componentWillMount is invoked on the client/server whereas componentDidMount is invoked only on the client. What does componentWillMount do to the server?

like image 685
BlueElixir Avatar asked Apr 27 '15 14:04

BlueElixir


People also ask

When should I use componentWillMount?

The componentWillMount lifecycle hook is an ideal choice when it comes to updating business logic, app configuration updates, and API calls. The componentWillMount() lifecycle hook is primarily used to implement server-side logic before the actual rendering happens, such as making an API call to the server.

What is componentWillMount in React?

ReactJS – componentWillMount() Method This function is generally called before the component gets loaded in the DOM tree. This method is called before the render() method is called, so it can be used to initialize the state but the constructor is preferred. This method is generally used in server-side rendering.

What is the difference between componentDidMount and componentDidUpdate?

The componentDidUpdate()is called after componentDidMount() and can be useful to perform some action when the state of the component changes. Parameters: Following are the parameter used in this function: prevProps: Previous props passed to the component. prevState: Previous state of the component.


1 Answers

componentWillMount is essentially the constructor. You can set instance properties that don't affect render, pull data from a store synchronously and setState with it, and other simple side effect free code you need to run when setting up your component.

It's rarely needed, and not at all with ES6 classes.

like image 137
Brigand Avatar answered Sep 30 '22 16:09

Brigand