In this blog Post you can get clear information about React Component Lifecycle methods. If require any support regarding react js services then contact us at hello@micropyramid.com
These are special methods provided by react Js. These methods are executed when specific condition( ex: component mounted to UI, State updated) triggered in the component.
Lifecycle methods which are called when component initialised.
Constructor is used to set initial state for component. for example in Counter component initial state of count is 0.
constructor(props) { this.state = { count: 0 }; }
This method will be called before component render to screen.
In this method you will see component on Screen. this method should be pure. Do not update state in this method.
This method will be called right after the render method. updating state in this method will re-render component.
This method is called whenever component recieves new props.
Note: this method is called several time. it is better to compare new props with old one.
componentWillReceiveProps(nextProps){ if (JSON.stringify(this.props.month) !== JSON.stringify(nextProps.month)){ // component recived new props. } }
In this method you can check whether re-rendering the component is neccessary or not. return false if you don't want to re-render component.
shouldComponentUpdate(nextProps, nextState){ // return a boolean value return true; }
This method will be called after shouldComponentUpdate(only if it returns true).
In this method updated component will be rendered to screen. with new data(or changes).
componentDidUpdate will be called after render method.
import React, { Component } from 'react'; class Lifecycle extends Component { constructor(props) { console.log('constructor called') super(props); this.state = { count: 0 }; this.Add = this.Add.bind(this); this.Remove = this.Remove.bind(this); } componentWillMount(){ console.log('componentWillMount called') } componentDidMount() { console.log('componentDidMount called') } Add(){ this.setState({count: this.state.count + 1}) } Remove(){ this.setState({count: this.state.count - 1}) } componentWillReceiveProps(){ console.log('componentWillReceiveProps called') } shouldComponentUpdate(){ console.log('shouldComponentUpdate called'); return true } componentWillUpdate(){ console.log('componentWillUpdate called') } componentDidUpdate(){ console.log('componentDidUpdate called'); } render() { console.log('render called'); return ( <div> <div> <h1>{this.state.count}</h1> </div> <button onClick={this.Add}> <span>+(Add)</span> </button> <button onClick={this.Remove}> <span>-(Remove)</span> </button> </div> ); } }
// Console Output: constructor called componentWillMount called render called componentDidMount called
// console Output: shouldComponentUpdate called componentWillUpdate called render called componentDidUpdate called
Micropyramid is a software development and cloud consulting partner for enterprise businesses across the world. We work on python, Django, Salesforce, Angular, Reactjs, React Native, MySQL, PostgreSQL, Docker, Linux, Ansible, git, amazon web services. We are Amazon and salesforce consulting partner with 5 years of cloud architect experience. We develop e-commerce, retail, banking, machine learning, CMS, CRM web and mobile applications.
Django-CRM :Customer relationship management based on Django
Django-blog-it : django blog with complete customization and ready to use with one click installer Edit
Django-webpacker : A django compressor tool
Django-MFA : Multi Factor Authentication
Docker-box : Web Interface to manage full blown docker containers and images
More...