GithubHelp home page GithubHelp logo

mian-ali / react-lifecycle-methods Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 20 KB

The Component Lifecycle

Home Page: https://gist.github.com/mian-ali/bec8986c0dc2b02a9bc86b802bae0ecb

License: GNU General Public License v3.0

reactjs lifecycle component lifecycle-components methods react react-lifecycle

react-lifecycle-methods's Introduction

The Component Lifecycle

Each component in React has a lifecycle which you can monitor and manipulate during its three main phases. The three phases are: Mounting, Updating, and Unmounting.

The component lifecycle goes through the following phase

  • Mounting
  • Updating
  • Unmounting

Mounting

The component rendered to the DOM for the first time. This is called mounting. These methods are called in the following order when an instance of a component is being created and inserted into the DOM.

  • constructor()
  • static getDerivedStateFromProps() rarely case use
  • render()
  • componentDidMount()

Updating

An update can be caused by changes to props or state. These methods are called in the following order when a component is being re-rendered

  • static getDerivedStateFromProps() rarely case use
  • shouldComponentUpdate() rarely case use
  • render()
  • getSnapshotBeforeUpdate() rarely case use
  • componentDidUpdate()

Unmounting

When component removed from DOM. This is called unmounting. Below method is called in this phase.

  • componentWillUnmount()

Component Lifecycle Diagram

lifecycle-diagram

Lifecycle Methods

constructor()

The constructor for a React component is called before it is mounted. The constructor call only once in whole lifecycle. You and set initial value for this component.

Constructors are only used for two purposes 1. Initializing local state by assigning an object to this.state 2. Binding event handler methods to an instance.

constructor(props){
	super(props);
	this.state = {qty: this.props.qty}
	this.clickHandling = this.clickHandling.bind(this);
}

static getDerivedStateFromProps()

It is invoked before calling render method. This method is used when state depends on props, whenever props is changed, then state automatically will be changed. getDerivedStateFromProps method use for both phase mounting and updating of the component. If null is returned then nothing is changed in the state.

static getDerivedStateFromProps(props, state) {
	if(props.qty !== state.qty) {
	  return {qty: props.qty}
	}
	return null;
}

render()

This method is the only required method in a class component. The render() function should be pure, meaning that it does not modify component state, which means it returns the same output each time it’s invoked.

render(){
    return(
      <div>
        <h2>Cart Items ({this.state.qty})</h2>
      </div>
    )
  }

}

componentDidMount()

This is invoked immediately after the component is mounted. If you load data using api then it right place for request data using api.

componentDidMount() {
    console.log('Execute this method after component render');
}

shouldComponentUpdate()

This is invoked before rendering when new props or state are being received. This methods return true or false according the component re-render or skipped. By default, this method returns true. This method is not called for the initial render.

shouldComponentUpdate(nextProps, nextState) {
    if(this.props.qty !== nextProps.qty) {
      console.log('should component update');
      return true;
    }
    return false;
}

componentDidUpdate()

This method immediately executed on the DOM when the component has been updated. Update occurs by changing state and props. This method is not called for the initial render. This is a good place for compare the current props to previous props.

componentDidUpdate(prevProps, prevState){
    if(this.props.productId !== prevProps.productId){
      console.log('component updated');
    }
}

componentWillUnmount()

This method immediately executed when the component is unmounted and destroy from the DOM. Means this method is called when a component is being removed from the DOM.

componentDidUpdate(prevProps, prevState){
    if(this.props.productId !== prevProps.productId){
      console.log('component updated');
    }
}

Posted By: @mian-ali (Ali Ahmad)

react-lifecycle-methods's People

Contributors

mian-ali avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.