SlideShare uma empresa Scribd logo
1 de 19
ReactJs Component
Lifecycle Methods
By: Chavan Ankush S.
What is lifecycle methods and why it is
important?
● Around us everything goes through a cycle of taking birth,
growing and at some point of time it will die.
● Consider trees, any software application, yourself, a div
container or UI component in a web browser, each of these
takes birth, grows by getting updates and dies.
● The lifecycle methods are various methods which are invoked
at different phases of the lifecycle of a component.
Four phases of a React component
● The React component goes through the following phases
○ Initialization
○ Mounting
○ Update
○ Unmounting
Visual representation of the phases and
the methods of ReactJs lifecycle.
Initialization
● In this phase the React component prepares for the upcoming
tough journey, by setting up the initial states and default
props, if any.
● The component is setting up the initial state in the constructor,
which can be changed later by using the setState method.
Mounting
● After preparing with basic needs, state and props, our React
Component is ready to mount in the browser DOM.
● This phase gives hook methods for before and after mounting
of components.
● The methods which gets called in this phase are
○ componentWillMount()
○ render()
○ componentDidMount()
Mounting conti…..
● componentWillMount()
○ This method is executed just before the React Component
is about to mount on the DOM.
○ This method is executed once in a lifecycle of a
component and before first render.
○ Usage: This method is used for initializing the states or
props,there is a huge debate going on to merge it with the
constructor.
Mounting conti…..
● render()
○ This method is mounts the component onto the browser.
○ This is a pure method, which means it gives the same
output every time the same input is provided.
Mounting conti…..
● componentDidMount()
○ This this is the hook method which is executed after the
component did mount on the dom.
○ This method is executed once in a lifecycle of a
component and after the first render.
○ As, in this method, we can access the DOM
○ Usage: this is the right method to integrate API
Update
● This phase starts when the react component has taken birth
on the browser and grows by receiving new updates.
● The component can be updated by two ways:
○ sending new props
○ updating the state.
Update Conti…...
● sending new props
○ The methods which gets called in this phase are :
■ componentWillReceiveProps()
■ shouldComponentUpdate()
■ componentWillUpdate()
■ render()
■ componentDidUpdate()
Update Conti…...
● componentWillReceiveProps()
○ This method gets executed when the props have changed
and is not first render.
○ Sometimes state depends on the props, hence whenever
props changes the state should also be synced,This is the
method where it should be done.
○ Usage: This is how the state can be kept synced with the
new props.
Update Conti…...
● shouldComponentUpdate()
○ This method tells the React that when the component
receives new props or state is being updated, should
React re-render or it can skip rendering?
● componentWillUpdate()
○ This method is executed only after the
shouldComponentUpdate() returns true
○ This method is only used to do the preparation for the
upcoming render, similar to componentWillMount() or
constructor.
Update Conti…...
● render()
○ This method is mounts the component onto the browser
● componentDidUpdate()
○ This method is executed when the new updated
component has been updated in the DOM.
○ This method is used to re trigger the third party libraries
used to make sure these libraries also update and reload
themselves.
Update Conti…...
● updating the state
○ The methods which gets called in this phase are :
■ shouldComponentUpdate()
■ componentWillUpdate()
■ render()
■ componentDidUpdate()
Unmounting
● In this phase, the component is not needed and the
component will get unmounted from the DOM.
● The method which is called in this phase :
○ componentWillUnmount()
Unmounting Conti….
● componentWillUnmount
○ This method is the last method in the lifecycle.
○ This is executed just before the component gets removed
from the DOM.
○ Usage: In this method, we do all the cleanups related to
the component.
■ For example, on logout, the user details and all the
auth tokens can be cleared before unmounting the
main component.
Any Question ?
Thank you...

Mais conteúdo relacionado

Mais procurados

Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSKnoldus Inc.
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_jsMicroPyramid .
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentationBojan Golubović
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Fabio Biondi
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMJimit Shah
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React NativeEric Deng
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming PrinciplesAndrii Lundiak
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.jsEmanuele DelBono
 

Mais procurados (20)

React hooks
React hooksReact hooks
React hooks
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
React js
React jsReact js
React js
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
react redux.pdf
react redux.pdfreact redux.pdf
react redux.pdf
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Reactjs
Reactjs Reactjs
Reactjs
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_js
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentation
 
Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022Redux Toolkit - Quick Intro - 2022
Redux Toolkit - Quick Intro - 2022
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
 
React Hooks
React HooksReact Hooks
React Hooks
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 

Semelhante a React-JS Component Life-cycle Methods

Optimizing your use of react life cycles by shedrack akintayo
Optimizing your use of react life cycles by shedrack akintayoOptimizing your use of react life cycles by shedrack akintayo
Optimizing your use of react life cycles by shedrack akintayoShedrack Akintayo
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycleJainul Musani
 
class based component.pptx
class based component.pptxclass based component.pptx
class based component.pptxsaikatsamanta49
 
React Workshop: Core concepts of react
React Workshop: Core concepts of reactReact Workshop: Core concepts of react
React Workshop: Core concepts of reactImran Sayed
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react jsStephieJohn
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit TestingEswara Kumar Palakollu
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 ThreadingDiego Grancini
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 
[Dec 1 meetup] upgrading microservices
[Dec 1 meetup] upgrading microservices[Dec 1 meetup] upgrading microservices
[Dec 1 meetup] upgrading microservicesMadhuri Yechuri
 
How can you force react components to rerender without calling the set state ppt
How can you force react components to rerender without calling the set state pptHow can you force react components to rerender without calling the set state ppt
How can you force react components to rerender without calling the set state pptBOSC Tech Labs
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples Ravi Mone
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfStephieJohn
 
Page life cycle IN ASP.NET
Page life cycle IN ASP.NETPage life cycle IN ASP.NET
Page life cycle IN ASP.NETSireesh K
 

Semelhante a React-JS Component Life-cycle Methods (20)

lifecycle.pptx
lifecycle.pptxlifecycle.pptx
lifecycle.pptx
 
Optimizing your use of react life cycles by shedrack akintayo
Optimizing your use of react life cycles by shedrack akintayoOptimizing your use of react life cycles by shedrack akintayo
Optimizing your use of react life cycles by shedrack akintayo
 
React workshop
React workshopReact workshop
React workshop
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
 
class based component.pptx
class based component.pptxclass based component.pptx
class based component.pptx
 
Life Cyrcle.pptx
Life Cyrcle.pptxLife Cyrcle.pptx
Life Cyrcle.pptx
 
Life Cycle hooks in VueJs
Life Cycle hooks in VueJsLife Cycle hooks in VueJs
Life Cycle hooks in VueJs
 
20120308 droid4me-paug presentation
20120308 droid4me-paug presentation20120308 droid4me-paug presentation
20120308 droid4me-paug presentation
 
React Workshop: Core concepts of react
React Workshop: Core concepts of reactReact Workshop: Core concepts of react
React Workshop: Core concepts of react
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit Testing
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
[Dec 1 meetup] upgrading microservices
[Dec 1 meetup] upgrading microservices[Dec 1 meetup] upgrading microservices
[Dec 1 meetup] upgrading microservices
 
How can you force react components to rerender without calling the set state ppt
How can you force react components to rerender without calling the set state pptHow can you force react components to rerender without calling the set state ppt
How can you force react components to rerender without calling the set state ppt
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples
 
Mobile Application Development class 006
Mobile Application Development class 006Mobile Application Development class 006
Mobile Application Development class 006
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
 
Web dynpro
Web dynproWeb dynpro
Web dynpro
 
Page life cycle IN ASP.NET
Page life cycle IN ASP.NETPage life cycle IN ASP.NET
Page life cycle IN ASP.NET
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

React-JS Component Life-cycle Methods

  • 2. What is lifecycle methods and why it is important? ● Around us everything goes through a cycle of taking birth, growing and at some point of time it will die. ● Consider trees, any software application, yourself, a div container or UI component in a web browser, each of these takes birth, grows by getting updates and dies. ● The lifecycle methods are various methods which are invoked at different phases of the lifecycle of a component.
  • 3. Four phases of a React component ● The React component goes through the following phases ○ Initialization ○ Mounting ○ Update ○ Unmounting
  • 4. Visual representation of the phases and the methods of ReactJs lifecycle.
  • 5. Initialization ● In this phase the React component prepares for the upcoming tough journey, by setting up the initial states and default props, if any. ● The component is setting up the initial state in the constructor, which can be changed later by using the setState method.
  • 6. Mounting ● After preparing with basic needs, state and props, our React Component is ready to mount in the browser DOM. ● This phase gives hook methods for before and after mounting of components. ● The methods which gets called in this phase are ○ componentWillMount() ○ render() ○ componentDidMount()
  • 7. Mounting conti….. ● componentWillMount() ○ This method is executed just before the React Component is about to mount on the DOM. ○ This method is executed once in a lifecycle of a component and before first render. ○ Usage: This method is used for initializing the states or props,there is a huge debate going on to merge it with the constructor.
  • 8. Mounting conti….. ● render() ○ This method is mounts the component onto the browser. ○ This is a pure method, which means it gives the same output every time the same input is provided.
  • 9. Mounting conti….. ● componentDidMount() ○ This this is the hook method which is executed after the component did mount on the dom. ○ This method is executed once in a lifecycle of a component and after the first render. ○ As, in this method, we can access the DOM ○ Usage: this is the right method to integrate API
  • 10. Update ● This phase starts when the react component has taken birth on the browser and grows by receiving new updates. ● The component can be updated by two ways: ○ sending new props ○ updating the state.
  • 11. Update Conti…... ● sending new props ○ The methods which gets called in this phase are : ■ componentWillReceiveProps() ■ shouldComponentUpdate() ■ componentWillUpdate() ■ render() ■ componentDidUpdate()
  • 12. Update Conti…... ● componentWillReceiveProps() ○ This method gets executed when the props have changed and is not first render. ○ Sometimes state depends on the props, hence whenever props changes the state should also be synced,This is the method where it should be done. ○ Usage: This is how the state can be kept synced with the new props.
  • 13. Update Conti…... ● shouldComponentUpdate() ○ This method tells the React that when the component receives new props or state is being updated, should React re-render or it can skip rendering? ● componentWillUpdate() ○ This method is executed only after the shouldComponentUpdate() returns true ○ This method is only used to do the preparation for the upcoming render, similar to componentWillMount() or constructor.
  • 14. Update Conti…... ● render() ○ This method is mounts the component onto the browser ● componentDidUpdate() ○ This method is executed when the new updated component has been updated in the DOM. ○ This method is used to re trigger the third party libraries used to make sure these libraries also update and reload themselves.
  • 15. Update Conti…... ● updating the state ○ The methods which gets called in this phase are : ■ shouldComponentUpdate() ■ componentWillUpdate() ■ render() ■ componentDidUpdate()
  • 16. Unmounting ● In this phase, the component is not needed and the component will get unmounted from the DOM. ● The method which is called in this phase : ○ componentWillUnmount()
  • 17. Unmounting Conti…. ● componentWillUnmount ○ This method is the last method in the lifecycle. ○ This is executed just before the component gets removed from the DOM. ○ Usage: In this method, we do all the cleanups related to the component. ■ For example, on logout, the user details and all the auth tokens can be cleared before unmounting the main component.