SlideShare une entreprise Scribd logo
1  sur  43
Core Concepts of
React
@imranhsayed
Imran Sayed
React Js
React is a javascript library for building
interactive User Interface( UI ).
2
3
Lorem Ipsum
Lorem IpsumLorem Ipsum
Lorem Ipsum Lorem Ipsum Lorem Ipsum
Lorem Ipsum Lorem Ipsum Lorem Ipsum
Root Element
<html>
Element
<body>
Element
<head>
Element
<title>
Element
<h1>
Element
<p>
Text Node
“Page title”
Text Node
“My heading”
Text Node
“My Paragraph
DOM
What makes React so
fast?
4
5
Virtual DOM
Virtual DOM
6
7
Virtual DOM
8
patch
Virtual DOM
9
Why is React fast?
10
Why is React so fast?
What is a
Virtual DOM?
A Javascript Object that is a
“virtual”, representation of the “real”
DOM.
11
Why use react?
● Updates and renders only the elements that
change/update in the DOM( hence quick rendering )
● Build encapsulated components that manage their own
state.
● React can also render on the server using Node and
powerful mobile apps using react native.
12
13
14
History of React
● Created by Jordan Walke, a software engineer at
Facebook
● First deployed on Facebook's newsfeed in 2011 and later
on Instagram.com in 2012
● Open-sourced at JSConf US in May 2013
● Facebook announced React Fiber, on April 18, 2017
● React 360 V1.0.0 was released to the public on April 19,
2017
15
1- Add React in a Minute (Using React Scripts )
2- Using create-react-app
Set up React App
16
Class Based Components
Extend React Component Class and
it requires us to use render()
React Components
Functional Based Components
Are pure JavaScript function that
accepts props as its argument, and
returns some JSX
17
Components allow you to split the UI into independent reusable pieces of
JS codes.
Class Based Components
Functional Based Components
18
JSX ( JavaScript-XML )
● JSX is a XML-like syntax extension to JavaScript that creates
React elements.
● Its makes your code human readable, otherwise we had to use
React.createElement()
● Gives us expressiveness of JavaScript along with HTML like
template syntax.
19
Examples of using JSX
20
What is a component
state?
● A State of a component is an object, that holds some
information that may change throughout the component
lifecycle.
21
State of a Component
22
Virtual DOM
State of a Component
23
Virtual DOM
State of a Component
24
Virtual DOM
State of a Component
25
Virtual DOM
setState()
State of a Component
26
Virtual DOM
setState()
State of a Component
27
Virtual DOM
setState()
State of a Component
28
setState()
patch
Virtual DOM
What is a component
state?
● A State of a component is an object, that holds some
information that may change throughout the component
lifecycle.
● We define initial state and then we just have to notify that the
state is changed and the react will automatically render the
those changes on the front end behind the scenes.
● Every time the state changes the changes get re rendered so
the UI( front end ) changes automatically.
29
Props
● Props are inputs to components.
● Single values or objects containing a set of values that are passed to
components on creation, using a naming convention similar to HTML-tag
attributes
● They are used to:
1-Pass custom data to your component.
2-Trigger state changes.
30
Important Points
● React may batch multiple setState() calls into a single update
for performance.
● Because this.props and this.state may be updated
asynchronously, you should not rely on their values for
calculating the next state
● State is local or encapsulated. It is not accessible to any
component other than the one that owns and sets it.
● A component may choose to pass its state down as props to
its child components
● Imagine a component tree as a waterfall of props, each
component’s state is like an additional water source that joins
it at an arbitrary point but also flows down
31
Difference between State & Props
Props
=Props is passed by the parent to the
Child components
-A component should never modify its
own props
Component State
=State is managed within a
component for internal
communication
- State can be modified using
setState() and when value of state
changes render() is called.
32
The props( properties ) and state are both JavaScript objects. Props are like an args
of a function and states are like local variables inside the function
Component LifeCycle
Mounting
- Component is constructed
with the given Props and
default state. This is done
inside constructor()
-Component came into the
DOM
Updating
- When the state of a
component is updated
33
Every React Component has a lifecycle of its own, which has different stages.
Unmounting
- Component is
removed from the
page
34
Constructor
● Overrides the constructor of React.Component
● Required only when we initialize state or bind methods.
E.g. this.handleClick = this.handleClick.bind(this);
● Call super(props) before any other statement.
● Do not call setState() inside it
35
getDerivedStateFromProp
s
● Invoked right before calling the render method
● Called both on the initial mount and on subsequent updates
● Should return an object to update the state, or null to update
nothing.
● Rare use cases where the state depends on changes in props
36
ComponentDidMount
● Load data from a remote endpoint
● Calling setState() here will trigger an extra rendering, but it will
happen before the browser updates the screen.
37
shouldComponentUpdate
● Exists as a performance optimization
● Not called for the initial render or when forceUpdate() is used.
● Use built-in PureComponent instead of writing
shouldComponentUpdate()
● PureComponent performs a shallow comparison of props and
state, and reduces the chance that you’ll skip a necessary
update.
38
render
● Required method in a class component
● Should return React Elements ( JSX ) or Arrays and fragments
or Portals or String and numbers or Booleans or null
● Should not modify component state
● It does not directly interact with the browser.
39
getSnapshotBeforeUpdat
e
● Invoked right before the most recently rendered output is
committed to e.g. the DOM
● Capture some information from the DOM (e.g. scroll position)
before it is potentially changed
● Returned value will be passed as a parameter to
componentDidUpdate()
● Example to use it: A chat thread that need to handle scroll
position in a special way.
40
ComponentDidUpdate
● Invoked immediately after updating occurs.
● Calling setState() here will trigger an extra rendering, but it will
happen before the browser updates the screen.
● setState() can be called but it must be wrapped in a condition.
● If getSnapshotBeforeUpdate() is implemented, snapshot
param will be available, else undefined.
41
ComponentWillUnmount
● invoked immediately before a component is unmounted.
● For e.g. Invalidating timers, canceling network requests, or
cleaning up any subscriptions.
● should not call setState() as component will never be
rerendered.
42
43
Thanks!
Any questions?
@imranhsayed
@imran_.sayed
youtube.com/c/ImranSayedDev

Contenu connexe

Tendances

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 

Tendances (20)

Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React js
React jsReact js
React js
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
React js Rahil Memon
React js Rahil MemonReact js Rahil Memon
React js Rahil Memon
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to React
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
ReactJs
ReactJsReactJs
ReactJs
 
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
 
Reactjs
Reactjs Reactjs
Reactjs
 
Reactjs
ReactjsReactjs
Reactjs
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
Using Dagger in a Clean Architecture project
Using Dagger in a Clean Architecture projectUsing Dagger in a Clean Architecture project
Using Dagger in a Clean Architecture project
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentation
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
React js
React jsReact js
React js
 
reactJS
reactJSreactJS
reactJS
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 

Similaire à React Workshop: Core concepts of react

Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
Flipkart
 

Similaire à React Workshop: Core concepts of react (20)

Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
 
ReactJS (1)
ReactJS (1)ReactJS (1)
ReactJS (1)
 
Globant development week / React Redux Rorkshop
Globant development week / React Redux RorkshopGlobant development week / React Redux Rorkshop
Globant development week / React Redux Rorkshop
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
 
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
 
Intro to React.js
Intro to React.jsIntro to React.js
Intro to React.js
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
class based component.pptx
class based component.pptxclass based component.pptx
class based component.pptx
 
ReactJS - A quick introduction to Awesomeness
ReactJS - A quick introduction to AwesomenessReactJS - A quick introduction to Awesomeness
ReactJS - A quick introduction to Awesomeness
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
React js
React jsReact js
React js
 
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
 

Plus de Imran Sayed

Digging Into Gutenberg
Digging Into GutenbergDigging Into Gutenberg
Digging Into Gutenberg
Imran Sayed
 

Plus de Imran Sayed (19)

Docker with WordPress
Docker with WordPressDocker with WordPress
Docker with WordPress
 
Why Progressive Web Apps For WordPress - WordCamp Finland
Why Progressive Web Apps For WordPress - WordCamp FinlandWhy Progressive Web Apps For WordPress - WordCamp Finland
Why Progressive Web Apps For WordPress - WordCamp Finland
 
Custom gutenberg block development in react
Custom gutenberg block development in reactCustom gutenberg block development in react
Custom gutenberg block development in react
 
Fastest Way of Creating Gutenberg Blocks - WordCamp Rochester
Fastest Way of Creating Gutenberg Blocks - WordCamp RochesterFastest Way of Creating Gutenberg Blocks - WordCamp Rochester
Fastest Way of Creating Gutenberg Blocks - WordCamp Rochester
 
Harness The Power Of ACF For Gatsby and WordPress
Harness The Power Of ACF For Gatsby and WordPressHarness The Power Of ACF For Gatsby and WordPress
Harness The Power Of ACF For Gatsby and WordPress
 
Improving Your Debugging Skills In WordPress
Improving Your Debugging Skills In WordPressImproving Your Debugging Skills In WordPress
Improving Your Debugging Skills In WordPress
 
Build Modern Web Applications with React and WordPress
Build Modern Web Applications with React and WordPressBuild Modern Web Applications with React and WordPress
Build Modern Web Applications with React and WordPress
 
Why progressive apps for WordPress - WordSesh 2020
Why progressive apps for WordPress - WordSesh 2020Why progressive apps for WordPress - WordSesh 2020
Why progressive apps for WordPress - WordSesh 2020
 
Digging Into Gutenberg
Digging Into GutenbergDigging Into Gutenberg
Digging Into Gutenberg
 
Fastest Way Of Creating Gutenberg Blocks With Minimal JavaScript Knowledge ...
Fastest Way Of Creating  Gutenberg Blocks  With Minimal JavaScript Knowledge ...Fastest Way Of Creating  Gutenberg Blocks  With Minimal JavaScript Knowledge ...
Fastest Way Of Creating Gutenberg Blocks With Minimal JavaScript Knowledge ...
 
Why progressive web apps for word press wc-ahemdabad
Why progressive web apps for word press wc-ahemdabadWhy progressive web apps for word press wc-ahemdabad
Why progressive web apps for word press wc-ahemdabad
 
Build fast word press site in react in 30 mins with frontity
Build fast word press site in react in 30 mins   with frontityBuild fast word press site in react in 30 mins   with frontity
Build fast word press site in react in 30 mins with frontity
 
Build Fast WordPress Site With Gatsby
Build Fast WordPress Site With GatsbyBuild Fast WordPress Site With Gatsby
Build Fast WordPress Site With Gatsby
 
Why Progressive Apps For WordPress?
Why Progressive Apps For WordPress?Why Progressive Apps For WordPress?
Why Progressive Apps For WordPress?
 
Creating Gutenberg Blocks With ACF
Creating Gutenberg Blocks With ACFCreating Gutenberg Blocks With ACF
Creating Gutenberg Blocks With ACF
 
Custom gutenberg block development with React
Custom gutenberg block development with ReactCustom gutenberg block development with React
Custom gutenberg block development with React
 
SSR with React - Connecting Next.js with WordPress
SSR with React - Connecting Next.js with WordPressSSR with React - Connecting Next.js with WordPress
SSR with React - Connecting Next.js with WordPress
 
React with WordPress : Headless CMS
React with WordPress : Headless CMSReact with WordPress : Headless CMS
React with WordPress : Headless CMS
 
Introduction to Gutenberg- Imran Sayed
Introduction to Gutenberg- Imran SayedIntroduction to Gutenberg- Imran Sayed
Introduction to Gutenberg- Imran Sayed
 

Dernier

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Dernier (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

React Workshop: Core concepts of react

  • 2. React Js React is a javascript library for building interactive User Interface( UI ). 2
  • 3. 3 Lorem Ipsum Lorem IpsumLorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Root Element <html> Element <body> Element <head> Element <title> Element <h1> Element <p> Text Node “Page title” Text Node “My heading” Text Node “My Paragraph DOM
  • 4. What makes React so fast? 4
  • 10. 10 Why is React so fast?
  • 11. What is a Virtual DOM? A Javascript Object that is a “virtual”, representation of the “real” DOM. 11
  • 12. Why use react? ● Updates and renders only the elements that change/update in the DOM( hence quick rendering ) ● Build encapsulated components that manage their own state. ● React can also render on the server using Node and powerful mobile apps using react native. 12
  • 13. 13
  • 14. 14
  • 15. History of React ● Created by Jordan Walke, a software engineer at Facebook ● First deployed on Facebook's newsfeed in 2011 and later on Instagram.com in 2012 ● Open-sourced at JSConf US in May 2013 ● Facebook announced React Fiber, on April 18, 2017 ● React 360 V1.0.0 was released to the public on April 19, 2017 15
  • 16. 1- Add React in a Minute (Using React Scripts ) 2- Using create-react-app Set up React App 16
  • 17. Class Based Components Extend React Component Class and it requires us to use render() React Components Functional Based Components Are pure JavaScript function that accepts props as its argument, and returns some JSX 17 Components allow you to split the UI into independent reusable pieces of JS codes.
  • 18. Class Based Components Functional Based Components 18
  • 19. JSX ( JavaScript-XML ) ● JSX is a XML-like syntax extension to JavaScript that creates React elements. ● Its makes your code human readable, otherwise we had to use React.createElement() ● Gives us expressiveness of JavaScript along with HTML like template syntax. 19
  • 21. What is a component state? ● A State of a component is an object, that holds some information that may change throughout the component lifecycle. 21
  • 22. State of a Component 22 Virtual DOM
  • 23. State of a Component 23 Virtual DOM
  • 24. State of a Component 24 Virtual DOM
  • 25. State of a Component 25 Virtual DOM setState()
  • 26. State of a Component 26 Virtual DOM setState()
  • 27. State of a Component 27 Virtual DOM setState()
  • 28. State of a Component 28 setState() patch Virtual DOM
  • 29. What is a component state? ● A State of a component is an object, that holds some information that may change throughout the component lifecycle. ● We define initial state and then we just have to notify that the state is changed and the react will automatically render the those changes on the front end behind the scenes. ● Every time the state changes the changes get re rendered so the UI( front end ) changes automatically. 29
  • 30. Props ● Props are inputs to components. ● Single values or objects containing a set of values that are passed to components on creation, using a naming convention similar to HTML-tag attributes ● They are used to: 1-Pass custom data to your component. 2-Trigger state changes. 30
  • 31. Important Points ● React may batch multiple setState() calls into a single update for performance. ● Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state ● State is local or encapsulated. It is not accessible to any component other than the one that owns and sets it. ● A component may choose to pass its state down as props to its child components ● Imagine a component tree as a waterfall of props, each component’s state is like an additional water source that joins it at an arbitrary point but also flows down 31
  • 32. Difference between State & Props Props =Props is passed by the parent to the Child components -A component should never modify its own props Component State =State is managed within a component for internal communication - State can be modified using setState() and when value of state changes render() is called. 32 The props( properties ) and state are both JavaScript objects. Props are like an args of a function and states are like local variables inside the function
  • 33. Component LifeCycle Mounting - Component is constructed with the given Props and default state. This is done inside constructor() -Component came into the DOM Updating - When the state of a component is updated 33 Every React Component has a lifecycle of its own, which has different stages. Unmounting - Component is removed from the page
  • 34. 34
  • 35. Constructor ● Overrides the constructor of React.Component ● Required only when we initialize state or bind methods. E.g. this.handleClick = this.handleClick.bind(this); ● Call super(props) before any other statement. ● Do not call setState() inside it 35
  • 36. getDerivedStateFromProp s ● Invoked right before calling the render method ● Called both on the initial mount and on subsequent updates ● Should return an object to update the state, or null to update nothing. ● Rare use cases where the state depends on changes in props 36
  • 37. ComponentDidMount ● Load data from a remote endpoint ● Calling setState() here will trigger an extra rendering, but it will happen before the browser updates the screen. 37
  • 38. shouldComponentUpdate ● Exists as a performance optimization ● Not called for the initial render or when forceUpdate() is used. ● Use built-in PureComponent instead of writing shouldComponentUpdate() ● PureComponent performs a shallow comparison of props and state, and reduces the chance that you’ll skip a necessary update. 38
  • 39. render ● Required method in a class component ● Should return React Elements ( JSX ) or Arrays and fragments or Portals or String and numbers or Booleans or null ● Should not modify component state ● It does not directly interact with the browser. 39
  • 40. getSnapshotBeforeUpdat e ● Invoked right before the most recently rendered output is committed to e.g. the DOM ● Capture some information from the DOM (e.g. scroll position) before it is potentially changed ● Returned value will be passed as a parameter to componentDidUpdate() ● Example to use it: A chat thread that need to handle scroll position in a special way. 40
  • 41. ComponentDidUpdate ● Invoked immediately after updating occurs. ● Calling setState() here will trigger an extra rendering, but it will happen before the browser updates the screen. ● setState() can be called but it must be wrapped in a condition. ● If getSnapshotBeforeUpdate() is implemented, snapshot param will be available, else undefined. 41
  • 42. ComponentWillUnmount ● invoked immediately before a component is unmounted. ● For e.g. Invalidating timers, canceling network requests, or cleaning up any subscriptions. ● should not call setState() as component will never be rerendered. 42