MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Component Lifecycle

Component Creation


When a React component is created, a number of functions are called:

If you are using React.createClass (ES5), 5 user defined functions are called
If you are using class Component extends React.Component (ES6), 3 user defined functions are called getDefaultProps() (ES5 only)

This is the first method called.

Prop values returned by this function will be used as defaults if they are not defined when the component is instantiated.

In the following example, this.props.name will be defaulted to Bob if not specified otherwise:

getDefaultProps() {
 return {
 initialCount: 0,
 name: 'Bob'
 };
}

getInitialState() (ES5 only)


This is the second method called.

The return value of getInitialState() defines the initial state of the React component. The React framework will call this function and assign the return value to this.state.

In the following example, this.state.count will be intialized with the value of this.props.initialCount:

getInitialState() {
 return {
 count : this.props.initialCount
 };
}

componentWillMount() (ES5 and ES6)


This is the third method called.

This function can be used to make final changes to the component before it will be added to the DOM.

componentWillMount() {
 ...
}

render() (ES5 and ES6)


This is the fourth method called.

The render() function should be a pure function of the component's state and props. It returns a single element which represents the component during the rendering process and should either be a representation of a native DOM component (e.g.

) or a composite component. If nothing should be rendered, it can return null or undefined.

This function will be recalled after any change to the component's props or state.

render() {
 return (
 <div>
 Hello, {this.props.name}!
 </div>
 );
}

componentDidMount() (ES5 and ES6)


This is the fifth method called.

The component has been mounted and you are now able to access the component's DOM nodes, e.g. via refs.

This method should be used for:

.Preparing timers
.Fetching data
.Adding event listeners
.Manipulating DOM elements


componentDidMount() {
 ...
}

ES6 Syntax


If the component is defined using ES6 class syntax, the functions getDefaultProps() and getInitialState() cannot be used.

Instead, we declare our defaultProps as a static property on the class, and declare the state shape and initial state in the constructor of our class. These are both set on the instance of the class at construction time, before any other React lifecycle function is called.

The following example demonstrates this alternative approach:

class MyReactClass extends React.Component {
 constructor(props){
 super(props);
 this.state = {
 count: this.props.initialCount
 };
 }
 upCount() {
 this.setState((prevState) => ({
 count: prevState.count + 1
 }));
 }
 render() {
 return (
<div>
Hello, {this.props.name}!<br />
 You clicked the button {this.state.count} times.<br />
 <button onClick={this.upCount}>Click here!</button>
 </div>
 );
 }
}
MyReactClass.defaultProps = {
 name: 'Bob',
 initialCount: 0
};

Replacing getDefaultProps()


Default values for the component props are specified by setting the defaultProps property of the class:

MyReactClass.defaultProps = {
 name: 'Bob',
 initialCount: 0
};

Replacing getInitialState()


The idiomatic way to set up the initial state of the component is to set this.state in the constructor:

constructor(props){
 super(props);
 this.state = {
 count: this.props.initialCount
 }
}


Conclusion

In this page (written and validated by ) you learned about React Component Lifecycle . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React Component Removal.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.