Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of having functions like componentWillMount in React.js?

I have been writing components in React.js recently. I have never had to use methods like componentWillMount and componentDidMount.

render is indispensable. getInitialState and other helper methods I wrote also come in handy. But not the two aforementioned lifecycle methods.

My current guess is that they are used for debugging? I can console.log out inside them:

componentWillMount: function() {
  console.log('component currently mounting');
},

componentDidMount: function() {
  console.log('component has mounted');
} 

Are there any other uses?

like image 514
Adi Sivasankaran Avatar asked May 05 '15 01:05

Adi Sivasankaran


People also ask

What is componentWillMount in functional component?

This is the only time it will fire on component render (componentWillMount) This means you can easily use and within functional components. This means that you can use both functions in the same useEffect function call.

Why do we make API calls in componentWillMount functions?

Calling API in constructor() or componentWillMount() is not a syntax error but increases code complexity and hampers performance. So, to avoid unnecessary re-rendering and code complexity, it's better to call API after render(), i.e componentDidMount().

What is the purpose of the render function in React?

React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML element. In the render() method, we can read props and state and return our JSX code to the root component of our app.

Why do we use shouldComponentUpdate () function in Reactjs?

The shouldComponentUpdate method allows us to exit the complex react update life cycle to avoid calling it again and again on every re-render. It only updates the component if the props passed to it changes.


2 Answers

componentDidMount is useful if you want to use some non-React JavaScript plugins. For example, there is a lack of a good date picker in React. Pickaday is beautiful and it just plain works out of the box. So my DateRangeInput component is now using Pickaday for the start and end date input and I hooked it up like so:

  componentDidMount: function() {     new Pikaday({       field: React.findDOMNode(this.refs.start),       format: 'MM/DD/YYYY',       onSelect: this.onChangeStart     });      new Pikaday({       field: React.findDOMNode(this.refs.end),       format: 'MM/DD/YYYY',       onSelect: this.onChangeEnd     });   }, 

The DOM needs to be rendered for Pikaday to hook up to it and the componentDidMount hook lets you hook into that exact event.

componentWillMount is useful when you want to do something programatically right before the component mounts. An example in one codebase I'm working on is a mixin that has a bunch of code that would otherwise be duplicated in a number of different menu components. componentWillMount is used to set the state of one specific shared attribute. Another way componentWillMount could be used is to set a behaviour of the component branching by prop(s):

  componentWillMount() {     let mode;     if (this.props.age > 70) {       mode = 'old';     } else if (this.props.age < 18) {       mode = 'young';     } else {       mode = 'middle';     }     this.setState({ mode });   } 
like image 60
Cymen Avatar answered Sep 22 '22 16:09

Cymen


componentDidMount only runs once and on the client side. This is important, especially if you're writing an isomorphic app (runs on both the client and server). You can use componentDidMount to perform tasks require you to have access to window or the DOM.

From Facebook's React Page

If you want to integrate with other JavaScript frameworks, set timers using setTimeout or setInterval, or send AJAX requests, perform those operations in this method.

componentWillMount has fewer use cases (I don't really use it), but it differs in that it runs both on the client and server side. You probably don't want to put event listeners or DOM manipulations here, since it will try to run on the server for no reason.

like image 22
Jon Avatar answered Sep 24 '22 16:09

Jon