SlideShare a Scribd company logo
ReactJS Component Lifecycle hooksReactJS Component Lifecycle hooks
with exampleswith examples
- Ravi Mone
Today's MenuToday's Menu
Order of execution of lifecycle hooks
1.GetDefaultProps
2.GetInitialState
3.ComponentWillMount
4.Render
5.ComponentDidMount
6.ComponentWillReceiveProps
7.ShouldComponentUpdate
8.ComponentWillUpdate
9.ComponentDidUpdate
10.ComponentWillUnMount
Order of execution of lifecycle hooksOrder of execution of lifecycle hooks
●Lets see the order of exectution of the components with a
demo
●https://plnkr.co/edit/QEyh4E1DSsl1Zlmbj6Wy?p=preview
getDefaultProps()getDefaultProps()
●Called once
●Used as defaults props to the component, if the component is not
passed with props and is used with this.props
getDefaultProps: function(){
return { /* something here */};
}
●https://plnkr.co/edit/3AnEup2KQLlFHCeA0Ffh?p=preview
getInitialState()getInitialState()
●Called once
●The getInitialState method enables to set the initial state value,
that is accessible inside the component via this.state.
getInitialState: function(){
return { /* something here */};
}
●https://plnkr.co/edit/QEyh4E1DSsl1Zlmbj6Wy?p=preview
ComponentWillMount()ComponentWillMount()
●Called once
●This hook has the ability to access the props (this.props) and state
(this.state). BUT NOT THE DOM.
syntax :
componentWillMount: function(){
/* something here */;
}
ex:
componentWillMount(){
console.log(this.props, this.state)
//This will be null
console.log(ReactDOM.findDOMNode(this))
}
●https://plnkr.co/edit/OExeRzWUQywpclAD2657?p=preview
render()render()
●Called evertime the state changes it value
i.e., when the state value is modified by
this.setState({
counter: <some value>
})
syntax :
render: function(){
return JSX DOM Or null/false
}
●Neither props nor state should be modified inside this function
●render function has to be pure, meaning that the same result is returned
every time the method is invoked
https://plnkr.co/edit/4se5p2vR9skoXsY3Mbo8?p=preview
componentDidMount()componentDidMount()
●Called once, after the render method is called
●This hook has the ability to access the props (this.props) and state
(this.state) and the DOM too.
syntax :
componentDidMount: function(){
/* something here */;
}
ex:
componentDidMount(){
console.log(this.props, this.state)
//This will return the ccurrent component's dom
console.log(ReactDOM.findDOMNode(this))
},
●Any DOM interactions should always happen in this phase not inside the
render method.
●https://plnkr.co/edit/pgsZXyeNHm7RrShs4b7a?p=preview
Quick GalanceQuick Galance
componentWillReceiveProps()componentWillReceiveProps()
●It will not be called on the initial render, but called in subsequent
renders
syntax :
componentWillReceiveProps: function(newProps){
/* something here */;
}
ex:
componentWillReceiveProps: function(newProps){
console.log(newProps, ReactDOM.findDOMNode(this))
},
●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
shouldComponentUpdate()shouldComponentUpdate()
●This is always called before the render method and enables to define
if a re-rendering is needed or can be skipped based on the bollean
vaue it returns.
syntax :
shouldComponentUpdate: function(nextProps, nextState){
// return a boolean value
return true;
}
ex:
shouldComponentUpdate(newProps, newState) {
console.log(newProps, newProps.myNumber <= 10)
return newProps.myNumber <= 10;
},
●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
componentWillUpdate()componentWillUpdate()
●componentWillUpdate gets called as soon as the the
shouldComponentUpdate returned true.
●Any state changes via this.setState are not allowed.
●strictly used to prepare for an upcoming update not trigger an
update itself.
syntax :
componentWillUpdate: function(nextProps, nextState){
// perform any preparations for an upcoming update
}
●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
componentDidUpdate()componentDidUpdate()
●componentWillUpdate gets called as soon as the the shouldComponentUpdate
returned true.
●Any state changes via this.setState are not allowed.
●Strictly used to prepare for an upcoming update not trigger an update itself.
syntax :
componentDidUpdate: function(prevProps, prevState){
// perform any preparations for an upcoming update
}
●Ex: Consider, you are using any third party library, lets says MaterialLite for
componets like checkbox, radio button...
And at some time, after the component is updated, the material may not be updating
(I once said the same “Firt Time it loaded,and god knows why it is not loading after
the compoent is touched/updated, :P ”), So this is your Hero
componentDidUpdate: function () {
//Reinitialize after the update
common.upgradeMaterialComponents.Init();
},
●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
componentWillUnMount()componentWillUnMount()
●Called once, after the render method is called and before the component is
removed from the DOM
●Used when needing to perform clean up operations, f.e. removing any
timers defined in componentDidMount.
syntax :
componentWillUnMount: function(){
/* something here */;
}
ex:
componentWillUnMount(){
console.log(this.props, this.state, ReactDOM.findDOMNode(this))
},
●https://plnkr.co/edit/xAJMb1pvEgcO7dUGrX9h?p=preview
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples

More Related Content

PDF
Runtime surgery
PPTX
Teorical1
PDF
GopherCon IL 2020 - Web Application Profiling 101
PPTX
Teorical 1
PDF
Pipeline interface
PPS
Unbounded
PPT
Unbounded
PPTX
Theorical 1
Runtime surgery
Teorical1
GopherCon IL 2020 - Web Application Profiling 101
Teorical 1
Pipeline interface
Unbounded
Unbounded
Theorical 1

What's hot (20)

PDF
Developing JavaScript Widgets
PDF
Apache Dispatch
PDF
Background Jobs - Com BackgrounDRb
ODP
SPRAT - Spreadsheet API Tester
PPT
Testing of javacript
ODP
LSA2 - 03 Http apache nginx
PDF
Android Libs - Retrofit
PDF
OB1K - New, Better, Faster, Devops Friendly Java container by Outbrain
PDF
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
PDF
Unit tests in node.js
PDF
JavaFX8 TestFX - CDI
PPTX
Ob1k presentation at Java.IL
PDF
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
PDF
"How to Use Bazel to Manage Monorepos: The Grammarly Front-End Team’s Experie...
PPTX
Binary Studio Academy: Concurrency in C# 5.0
ODP
Java 9 - Part1: New Features (Not Jigsaw Modules)
PPTX
The Gradle in Ratpack: Dissected
PDF
iOS Behavior-Driven Development
PDF
Java 13 Updates
PDF
Apple Templates Considered Harmful
Developing JavaScript Widgets
Apache Dispatch
Background Jobs - Com BackgrounDRb
SPRAT - Spreadsheet API Tester
Testing of javacript
LSA2 - 03 Http apache nginx
Android Libs - Retrofit
OB1K - New, Better, Faster, Devops Friendly Java container by Outbrain
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
Unit tests in node.js
JavaFX8 TestFX - CDI
Ob1k presentation at Java.IL
Photon Server Deep Dive - View from Implmentation of PhotonWire, Multiplayer ...
"How to Use Bazel to Manage Monorepos: The Grammarly Front-End Team’s Experie...
Binary Studio Academy: Concurrency in C# 5.0
Java 9 - Part1: New Features (Not Jigsaw Modules)
The Gradle in Ratpack: Dissected
iOS Behavior-Driven Development
Java 13 Updates
Apple Templates Considered Harmful
Ad

Viewers also liked (18)

PDF
Our take on Digital Analytics in New Zealand in 2017
DOCX
Namenindex Memori Boeck - Cornelis Pieter Sorgdrager
PDF
Matt Dixon_Unity 5 Lighting Fundamentals
PDF
The future of the new Generation - New technologies, new jobs, new environment
PPT
Expert Discussions on Antibody-Based Therapy in AML: Will New Technologies Ma...
PDF
Death Of the PMO
PDF
The Cassandra Distributed Database
PPTX
SAPUI5 & OpenUI5 for SAP InnoJam
PPTX
Full Stack Developers are no Longer Required
ODP
Hadoop and Cassandra at Rackspace
PDF
Cassandra @Formspring
PPTX
From APM to Full Stack with Blue Medora [FutureStack16]
DOCX
Unityware's Synthia is a business process AI technology game changer.
PPTX
How Docker Accelerates Continuous Development at Codefresh: Containers #101 M...
PPT
Agile Design with Scrum - Fast, furious & effective
PPTX
Vuejs Angularjs e Reactjs. Veja as diferenças de cada framework!
PDF
Apache Spark PDF
PDF
How to Become an Equestrian Coach
Our take on Digital Analytics in New Zealand in 2017
Namenindex Memori Boeck - Cornelis Pieter Sorgdrager
Matt Dixon_Unity 5 Lighting Fundamentals
The future of the new Generation - New technologies, new jobs, new environment
Expert Discussions on Antibody-Based Therapy in AML: Will New Technologies Ma...
Death Of the PMO
The Cassandra Distributed Database
SAPUI5 & OpenUI5 for SAP InnoJam
Full Stack Developers are no Longer Required
Hadoop and Cassandra at Rackspace
Cassandra @Formspring
From APM to Full Stack with Blue Medora [FutureStack16]
Unityware's Synthia is a business process AI technology game changer.
How Docker Accelerates Continuous Development at Codefresh: Containers #101 M...
Agile Design with Scrum - Fast, furious & effective
Vuejs Angularjs e Reactjs. Veja as diferenças de cada framework!
Apache Spark PDF
How to Become an Equestrian Coach
Ad

Similar to ReactJS Component Lifecycle hooks with examples (20)

PDF
Optimizing your use of react life cycles by shedrack akintayo
PPTX
React-JS Component Life-cycle Methods
PDF
React js
PPTX
Dyanaimcs of business and economics unit 2
PPTX
class based component.pptx
PPTX
React & Redux for noobs
DOCX
Component Life Cycle in React.docx
PDF
Understanding React hooks | Walkingtree Technologies
PPTX
React, Flux and more (p1)
PPTX
React workshop
PDF
React – Let’s “Hook” up
PPTX
React Workshop: Core concepts of react
PPTX
React and Flux life cycle with JSX, React Router and Jest Unit Testing
PPTX
ReactJS (1)
PDF
React Internals - How understanding React implementation can help us write be...
PPTX
Presentation on "An Introduction to ReactJS"
PPTX
O365 Developer Bootcamp NJ 2018 - Material
PPTX
React Hooks Best Practices in 2022.pptx
PDF
React js t6 -lifecycle
PDF
How to build a react native app with the help of react native hooks
Optimizing your use of react life cycles by shedrack akintayo
React-JS Component Life-cycle Methods
React js
Dyanaimcs of business and economics unit 2
class based component.pptx
React & Redux for noobs
Component Life Cycle in React.docx
Understanding React hooks | Walkingtree Technologies
React, Flux and more (p1)
React workshop
React – Let’s “Hook” up
React Workshop: Core concepts of react
React and Flux life cycle with JSX, React Router and Jest Unit Testing
ReactJS (1)
React Internals - How understanding React implementation can help us write be...
Presentation on "An Introduction to ReactJS"
O365 Developer Bootcamp NJ 2018 - Material
React Hooks Best Practices in 2022.pptx
React js t6 -lifecycle
How to build a react native app with the help of react native hooks

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectroscopy.pptx food analysis technology
Diabetes mellitus diagnosis method based random forest with bat algorithm

ReactJS Component Lifecycle hooks with examples

  • 1. ReactJS Component Lifecycle hooksReactJS Component Lifecycle hooks with exampleswith examples - Ravi Mone
  • 2. Today's MenuToday's Menu Order of execution of lifecycle hooks 1.GetDefaultProps 2.GetInitialState 3.ComponentWillMount 4.Render 5.ComponentDidMount 6.ComponentWillReceiveProps 7.ShouldComponentUpdate 8.ComponentWillUpdate 9.ComponentDidUpdate 10.ComponentWillUnMount
  • 3. Order of execution of lifecycle hooksOrder of execution of lifecycle hooks ●Lets see the order of exectution of the components with a demo ●https://plnkr.co/edit/QEyh4E1DSsl1Zlmbj6Wy?p=preview
  • 4. getDefaultProps()getDefaultProps() ●Called once ●Used as defaults props to the component, if the component is not passed with props and is used with this.props getDefaultProps: function(){ return { /* something here */}; } ●https://plnkr.co/edit/3AnEup2KQLlFHCeA0Ffh?p=preview
  • 5. getInitialState()getInitialState() ●Called once ●The getInitialState method enables to set the initial state value, that is accessible inside the component via this.state. getInitialState: function(){ return { /* something here */}; } ●https://plnkr.co/edit/QEyh4E1DSsl1Zlmbj6Wy?p=preview
  • 6. ComponentWillMount()ComponentWillMount() ●Called once ●This hook has the ability to access the props (this.props) and state (this.state). BUT NOT THE DOM. syntax : componentWillMount: function(){ /* something here */; } ex: componentWillMount(){ console.log(this.props, this.state) //This will be null console.log(ReactDOM.findDOMNode(this)) } ●https://plnkr.co/edit/OExeRzWUQywpclAD2657?p=preview
  • 7. render()render() ●Called evertime the state changes it value i.e., when the state value is modified by this.setState({ counter: <some value> }) syntax : render: function(){ return JSX DOM Or null/false } ●Neither props nor state should be modified inside this function ●render function has to be pure, meaning that the same result is returned every time the method is invoked https://plnkr.co/edit/4se5p2vR9skoXsY3Mbo8?p=preview
  • 8. componentDidMount()componentDidMount() ●Called once, after the render method is called ●This hook has the ability to access the props (this.props) and state (this.state) and the DOM too. syntax : componentDidMount: function(){ /* something here */; } ex: componentDidMount(){ console.log(this.props, this.state) //This will return the ccurrent component's dom console.log(ReactDOM.findDOMNode(this)) }, ●Any DOM interactions should always happen in this phase not inside the render method. ●https://plnkr.co/edit/pgsZXyeNHm7RrShs4b7a?p=preview
  • 10. componentWillReceiveProps()componentWillReceiveProps() ●It will not be called on the initial render, but called in subsequent renders syntax : componentWillReceiveProps: function(newProps){ /* something here */; } ex: componentWillReceiveProps: function(newProps){ console.log(newProps, ReactDOM.findDOMNode(this)) }, ●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
  • 11. shouldComponentUpdate()shouldComponentUpdate() ●This is always called before the render method and enables to define if a re-rendering is needed or can be skipped based on the bollean vaue it returns. syntax : shouldComponentUpdate: function(nextProps, nextState){ // return a boolean value return true; } ex: shouldComponentUpdate(newProps, newState) { console.log(newProps, newProps.myNumber <= 10) return newProps.myNumber <= 10; }, ●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
  • 12. componentWillUpdate()componentWillUpdate() ●componentWillUpdate gets called as soon as the the shouldComponentUpdate returned true. ●Any state changes via this.setState are not allowed. ●strictly used to prepare for an upcoming update not trigger an update itself. syntax : componentWillUpdate: function(nextProps, nextState){ // perform any preparations for an upcoming update } ●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
  • 13. componentDidUpdate()componentDidUpdate() ●componentWillUpdate gets called as soon as the the shouldComponentUpdate returned true. ●Any state changes via this.setState are not allowed. ●Strictly used to prepare for an upcoming update not trigger an update itself. syntax : componentDidUpdate: function(prevProps, prevState){ // perform any preparations for an upcoming update } ●Ex: Consider, you are using any third party library, lets says MaterialLite for componets like checkbox, radio button... And at some time, after the component is updated, the material may not be updating (I once said the same “Firt Time it loaded,and god knows why it is not loading after the compoent is touched/updated, :P ”), So this is your Hero componentDidUpdate: function () { //Reinitialize after the update common.upgradeMaterialComponents.Init(); }, ●https://plnkr.co/edit/wNVTeSSINT401JifucLb?p=preview
  • 14. componentWillUnMount()componentWillUnMount() ●Called once, after the render method is called and before the component is removed from the DOM ●Used when needing to perform clean up operations, f.e. removing any timers defined in componentDidMount. syntax : componentWillUnMount: function(){ /* something here */; } ex: componentWillUnMount(){ console.log(this.props, this.state, ReactDOM.findDOMNode(this)) }, ●https://plnkr.co/edit/xAJMb1pvEgcO7dUGrX9h?p=preview