React Training
  • React JS Library
  • Roadmap
  • Training OutLine
  • React js basics
    • Understanding React JS
    • React JS a framework?
    • Setting Up React
    • Say Hello to React
    • Everything is a Component
    • Create-react-app
  • React Building Blocks
    • JSX and Babel
    • One Way Data Flow
    • Virtual DOM
    • V of MVC
    • React Terminology
    • React Tooling
  • Day 01
    • Day 01 OutLine
    • All About JSX
    • React Tools/ npm & webpack
    • Introduction of Webpack
    • Hello world using webpack
      • Webpack Setting up with React
    • NPM Scripts | Package JSON
      • Package.json file
    • React JS Principles
      • One Way Data Flow
      • V of MVC
      • Virtual DOM
    • Create React App - Part-1
    • Create React App - Part-2
  • Day 02
    • Quick Recap
      • Quiz
    • State & Props
      • State & Props in Depth
      • State Vs Props | Compare
    • React LifeCycle Methods
      • React LifeCycle Methods for V-0.16 x
    • Constructor | Lifecycle
    • Write Flicker App | First App
  • Day 03
    • Quick Recap
    • Life Cycle Flow
      • Birth and Mounting
      • Initlization and Construction
      • Pre Mounting
      • Render Method
      • componentDidMount
    • Type of React Components
      • Examples- Quick Compare
      • Class and Functional components
      • Functional Components
    • YouTube application
      • Component Design
    • All in One LifeCycle
  • Assignment
    • React App development
  • Day 04
    • Quick Recap on Lifecycle
    • Lifecycle deprecated/New Methods
      • New Lifecycle Methods
    • Lets Build App Netflix | Mock
  • Assignment
    • Github battle App | Assignment
  • Day 05
    • Quick Recap : Hooks
    • ES6 Features | Hands-on
      • ES6 Code Examples
    • Next Stop - React Router | SPA
      • Code examples | Router
      • React Router Building Blocks
      • Application using react-router-dom
  • Day 06
    • Router V4 | Quick Recap
    • ES2015 | 16 Quick Recap
    • LifeCycle Methods -Part-1
    • LifeCycle Methods -Part-2
  • Day 07
    • Quick Recap | New Lifecycle
    • Quick Recap | React Routing
    • Context API | React JS
      • component with context APIs
      • Context API | examples
    • App using Hooks/Context APIs
  • Assignment
    • Assignments
  • State Management Day-08
    • Quick Recap
    • Managing React State
      • What is Redux
      • Understanding Redux
      • Hello World "Redux"
  • React Redux Day - 09
    • Redux State Manager
    • Redux Redux Development
    • Simple Application | Redux
  • Redux Live Application Day -10
    • Redux with existing Application
      • Redux with React App
      • Lets Build More Apps
      • Should I use Redux from Dan
    • Quick Look at JS in React
    • Learn By Reading
  • miscellaneous Items - Day 11
    • Hooks useReducer
    • Hooks useContext
    • Hooks useRef
    • Hooks useEffect
    • Hooks useState
    • Lazy Loading and code splitting
    • Styling React Component
  • React Next Step - Day 12
    • Topics
    • Jest and Enjyme Testing
    • Examples: Testing
  • React Native
    • What is React Native
    • Setting up Environment
      • Linux Systems
    • React Native Hello World App
    • React Native Architecture
    • React Native Vs Native
    • Expo Cli or React Native CLI
  • React Native core Fundamental
    • React Native "Hello World"
    • Course OutLine
    • Getting started with Expo App
    • Layout with Flexbox
    • Working with Styles and Events
    • Manging Component State and Props
    • Build Simple Task List Application
  • What to Debug & How to Debug
    • Debug React Native Application
Powered by GitBook
On this page

Was this helpful?

  1. Day 06

LifeCycle Methods -Part-1

PreviousES2015 | 16 Quick RecapNextLifeCycle Methods -Part-2

Last updated 5 years ago

Was this helpful?

Here is our new Picture of LifeCycle Methods

New life-cycle methods are:

  • static getDerivedStateFromProps()

  • getSnapshotBeforeUpdate()

Deprecated methods are:

  • componentWillMount became, UNSAFE_componentWillMount

  • componentWillReceiveProps became, UNSAFE_componentWillReceiveProps

  • componentWillUpdate became, UNSAFE_componentWillUpdate

These deprecated functions will be removed from react 17 version.

Let’s play with these new component life-cycles.

To understand the life-cycle functions, I have logged some text in side these functions. You can check the code. Here, we have two components. One is parent and another is it’s child. By clicking on “Add Child Component” button we can add the child component. We have separate buttons for updating the parent and the child individually.

Let’s start with adding a child component. When we click on the button to add the child, on the console we can see:console from google chrome — initial rendering

Child get mounted. While mounting, it called functions in below sequence:

  1. constructor

  2. getDerivedStateFromProps

  3. render

  4. componentDidMount

Note that, getDerivedStateFromProps has been called during initial rendering. You can see the console, it is printing two objects. Here, I have logged two arguments of getDerivedStateFromProps.

static getDerivedStateFromProps(nextProps, prevState) {
     log("getDerivedStateFromProps from Child");
     log(nextProps);
     log(prevState);
     return null;
}

Note:- During initial rendering, nextProps value will be a blank object in getDerivedStateFromProps.

Now, click on “Update Parent Component” button and check the log:console from google chrome — update parent component

Note:- When we updated the parent component then also child component is calling getDerivedStateFromProps even if props have not changed.

So the sequence of calling these function while updating the parent component is:

  1. getDerivedStateFromProps

  2. shouldComponentUpdate

  3. render

  4. getSnapshotBeforeUpdate

  5. componentDidUpdate

As we can see in the console, when child component get updated, it goes through below life-cycle functions.

  1. shouldComponentUpdate

  2. render

  3. getSnapshotBeforeUpdate

  4. componentDidUpdate

What we can see here? Child component didn’t call the getDerivedStateFromProps while updating itself (in react v16.3). Wow…interesting…isn’t it?

In short, calling this.setState() generally doesn’t triggergetDerivedStateFromProps().(only in React v16.3)

We need to remember this point while using React v16.3.

In React v16.4,

Even if you call setState in child component, it will call getDerivedStateFromProps() before shouldComponentUpdate() .Update child in React v16.4

Let’s summarize these new life-cycle functions

static getDerivedStateFromProps(nextProps, prevState)

  • It will be replacement for both componentWillMount and componentWillReceiveProps.

  • It use to take next props and previous state as arguments.

  • It should return an object to update state.

  • And it should return null to indicate that the new props do not require any state updates.

Edit:- There is some changes in React v16.4.0 for this life-cycle method. I will be updating this blog with these changes very soon.

getSnapshotBeforeUpdate()

  • It is invoked right before the most recently rendered output is committed to the DOM.

  • It takes previous props and previous state as an argument.

  • It enables your component to capture current values, let say scroll position, before they are potentially changed.

  • Any value returned by this life-cycle will be passed as a parameter to componentDidUpdate().

In React 16.3, new component life-cycles has been introduced. As per , these life-cycle methods are replacing existing ones.

Now, click on “Update Child Component” button and see the log:console from google chrome — update child component

copy of -

reactjs official doc
https://medium.com/@sgobinda007/playing-with-component-lifecycle-methods-of-react-16-3-4c946c1e6982