SlideShare a Scribd company logo
Introduction
to React Vijesh M. Nair
Assistant Professor
Dept. of CSE (AI-ML)
React
Js
React is a javascript library for
building interactive User Interface( UI
).
2
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Lorem
Ipsum
Root
Element
<html
>
Elemen
t
<body
>
Elemen
t
<head
>
Elemen
t<title
>
Elemen
t <h1
>
Elemen
t <p
>
Text
Node
“Page
title”
Text
Node
“My
heading”
Text
Node
“My
Paragraph
DOM
3
What makes React so
fast?
4
Virtual DOM
5
Virtual DOM
6
Virtual DOM
7
patch
8
Virtual DOM
Why is React
fast?
9
Why is React so
fast?
10
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
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
13
1- Add React in a Minute (Using React Scripts
) 2- Using create-react-app
3 Using Parcel
4 Using Webpack and Babel
Set up React
App
14
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
Components allow you to split the UI into independent reusable pieces
of JS codes.
15
Class Based Components
Functional Based Components
16
JSX ( JavaScript-
XML )
● JSX is a XML-like syntax extension to JavaScript that creates
React elements.
● Its makes your code human readable, we had to use
React.createElement()
● Gives us expressiveness of JavaScript along with HTML like
template syntax.
17
Examples of using
JSX
18
State of a
Component Virtual DOM
19
State of a
Component Virtual DOM
20
State of a
Component Virtual DOM
21
State of a
Component Virtual DOM
setState()
22
State of a
Component Virtual DOM
setState()
23
State of a
Component Virtual DOM
setState(
)
24
State of a
Component
setState(
)
patch
25
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 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.
26
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.
27
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
28
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.
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
29
Parent, Child &
Nested Components
30
Create & Handle
Routes
31
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
Every React Component has a lifecycle of its own, which has different
stages.
32
Unmounting
- Component is
removed from
the page
33
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
34
getDerivedStateFromProps
● 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
35
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.
36
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.
37
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.
38
getSnapshotBeforeUpdate
● 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.
39
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.
40
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.
41
Handling forms and
input values
42
Arrow functions and
Spread Operators
43
Reconciliation
● When a component’s props or state change,
React decides whether an actual DOM update is
necessary
by comparing,
newly returned element = previously rendered one. ?
When they are not equal, React will update the DOM
44
HOC
● A function that takes a component and returns a
new component.
● Advanced technique in React for reusing component
logic.
● Not part of the React API
● A pattern that emerges from React’s compositional
nature.
45
Pure Components
● React.PureComponent implements it with a shallow
prop and state comparison and does not re-render if the
they don’t change.
● So it handles shouldComponentUpdate() for you
● Does not re-render if nextState = prevState
46
Memo
● React.memo is a higher order component.
● Similar to React.PureComponent but for functional based
components instead of classes.
● If your function component renders the same result
given the same props, you can wrap it in a call to
React.memo
47
Use of
Refs
● React.createRef creates a reference that can be
attached to React elements via the ref attribute.
● Avoid using jQuery as you have to import the jQuery
library. as we’re going to manipulate the dom
● Compared to using jQuery, elements can be
faster referenced in a React way using Refs
● Refs are not present during the initial render
. You can
initialize it in constructor. However you must use it
inside one of the React’s lifecycle events such as
componentDidMount or componentDidUpdate.
48
49
Context
● Universal centralized data for your application.
● Used to share data, such as the current authenticated
user,
theme, or preferred language, between components.
● We can avoid passing props through
intermediate elements
50
1-Create
Context
51
2-Wrap in
Provider and
pass the
state to child
component
3-Wrap in Consumer and
receive context in Child
Components
Pros of
Context
● Set context on top and all the components in the
middle don’t have to know anything about it
● The one down at the bottom can access it
● Fills the same need as redux.
54
Cons of
Context
● Makes debugging difficult
● Difficult to figure out what caused the error when you
cannot see the data in the child components that is not
importing Context
● You have to look at all the components which are
Consuming it to figure out which one of them caused
the problem
● Use context only when you have to.
55
Difference between
Component state & Store state
Component State
=State is managed
within a component
- State value can
change, and then
render function is called.
Store state
=Store State is updated,
by the reducer, when
an action is triggered.
56
57

More Related Content

PPTX
React workshop
PPTX
React Workshop: Core concepts of react
PPTX
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
PPTX
Getting started with react &amp; redux
PDF
React Interview Question & Answers PDF By ScholarHat
PPTX
[Final] ReactJS presentation
PPTX
Dyanaimcs of business and economics unit 2
PDF
ReactJS - A quick introduction to Awesomeness
React workshop
React Workshop: Core concepts of react
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
Getting started with react &amp; redux
React Interview Question & Answers PDF By ScholarHat
[Final] ReactJS presentation
Dyanaimcs of business and economics unit 2
ReactJS - A quick introduction to Awesomeness

Similar to 2.React tttttttttttttttttttttttttttttttt (20)

PPTX
Reactjs
PPTX
ReactJS (1)
PPTX
React and Flux life cycle with JSX, React Router and Jest Unit Testing
PPTX
React.js - The Dawn of Virtual DOM
PPTX
Presentation on "An Introduction to ReactJS"
PDF
ReactJS presentation
PDF
react hook and wesite making structure ppt
PPTX
PDF
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
PDF
Introduction Web Development using ReactJS
PPTX
ReactJS presentation.pptx
PPT
PDF
ReactJS for Programmers
PPTX
React State vs Props Introduction & Differences.pptx
PDF
REACTJS.pdf
PDF
React JS and why it's awesome
PPTX
reactJS
PPTX
PDF
react.pdf
PPTX
ReactJS Code Impact
Reactjs
ReactJS (1)
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React.js - The Dawn of Virtual DOM
Presentation on "An Introduction to ReactJS"
ReactJS presentation
react hook and wesite making structure ppt
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
Introduction Web Development using ReactJS
ReactJS presentation.pptx
ReactJS for Programmers
React State vs Props Introduction & Differences.pptx
REACTJS.pdf
React JS and why it's awesome
reactJS
react.pdf
ReactJS Code Impact
Ad

More from MrVMNair (19)

PPTX
Advanced_HTML_CSS_Workshop_Final .pptx
PPTX
JAVA TUTORIAL llllllllllllllllllllllllll.pptx
PPT
4.Hardware concepts, Software Concept & Middleware.ppt
PPT
3. Distributed System Models ssssssssssssssssss.ppt
PPTX
2. Types of distributed systems ssssssssss.pptx
PPTX
1.Distributed Systems-Characterization,Issues, Goals.pptx
PPTX
1. Fundamentals of React ttttttttttttttt
PPT
Needham Schroeder rrrrrrrrrrrrrrrrrrrrrr
PPTX
1. Kerberos is an auth protocol llllllllllllllllllllll
PPTX
Google-Android Basics with Compose process document (3).pptx
PPT
Chapter_01_Introduction Two differen.ppt
PPTX
webpack introductionNotice Demystifyingf
PPT
DIPsadasdasfsdfsdfdfasdfsdfsdgsdgdsfgdfgfdg
PPTX
Lecture05.pptx
PPTX
Event Loop Node js.pptx
PPTX
EN Childhood Anxiety Disorder by Slidesgo.pptx
PPT
15998595.ppt
PPT
6.3 c-Functions.ppt
PPT
Chapter 1_NG_2020.ppt
Advanced_HTML_CSS_Workshop_Final .pptx
JAVA TUTORIAL llllllllllllllllllllllllll.pptx
4.Hardware concepts, Software Concept & Middleware.ppt
3. Distributed System Models ssssssssssssssssss.ppt
2. Types of distributed systems ssssssssss.pptx
1.Distributed Systems-Characterization,Issues, Goals.pptx
1. Fundamentals of React ttttttttttttttt
Needham Schroeder rrrrrrrrrrrrrrrrrrrrrr
1. Kerberos is an auth protocol llllllllllllllllllllll
Google-Android Basics with Compose process document (3).pptx
Chapter_01_Introduction Two differen.ppt
webpack introductionNotice Demystifyingf
DIPsadasdasfsdfsdfdfasdfsdfsdgsdgdsfgdfgfdg
Lecture05.pptx
Event Loop Node js.pptx
EN Childhood Anxiety Disorder by Slidesgo.pptx
15998595.ppt
6.3 c-Functions.ppt
Chapter 1_NG_2020.ppt
Ad

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Sustainable Sites - Green Building Construction
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Well-logging-methods_new................
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Project quality management in manufacturing
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Geodesy 1.pptx...............................................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
web development for engineering and engineering
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
composite construction of structures.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Sustainable Sites - Green Building Construction
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Well-logging-methods_new................
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Project quality management in manufacturing
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Geodesy 1.pptx...............................................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
Internet of Things (IOT) - A guide to understanding
web development for engineering and engineering
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Embodied AI: Ushering in the Next Era of Intelligent Systems

2.React tttttttttttttttttttttttttttttttt

  • 1. Introduction to React Vijesh M. Nair Assistant Professor Dept. of CSE (AI-ML)
  • 2. React Js React is a javascript library for building interactive User Interface( UI ). 2
  • 4. What makes React so fast? 4
  • 10. Why is React so fast? 10
  • 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. 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 13
  • 14. 1- Add React in a Minute (Using React Scripts ) 2- Using create-react-app 3 Using Parcel 4 Using Webpack and Babel Set up React App 14
  • 15. 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 Components allow you to split the UI into independent reusable pieces of JS codes. 15
  • 16. Class Based Components Functional Based Components 16
  • 17. JSX ( JavaScript- XML ) ● JSX is a XML-like syntax extension to JavaScript that creates React elements. ● Its makes your code human readable, we had to use React.createElement() ● Gives us expressiveness of JavaScript along with HTML like template syntax. 17
  • 19. State of a Component Virtual DOM 19
  • 20. State of a Component Virtual DOM 20
  • 21. State of a Component Virtual DOM 21
  • 22. State of a Component Virtual DOM setState() 22
  • 23. State of a Component Virtual DOM setState() 23
  • 24. State of a Component Virtual DOM setState( ) 24
  • 26. 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 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. 26
  • 27. 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. 27
  • 28. 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 28
  • 29. 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. 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 29
  • 30. Parent, Child & Nested Components 30
  • 32. 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 Every React Component has a lifecycle of its own, which has different stages. 32 Unmounting - Component is removed from the page
  • 33. 33
  • 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 34
  • 35. getDerivedStateFromProps ● 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 35
  • 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. 36
  • 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. 37
  • 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. 38
  • 39. getSnapshotBeforeUpdate ● 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. 39
  • 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. 40
  • 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. 41
  • 44. Reconciliation ● When a component’s props or state change, React decides whether an actual DOM update is necessary by comparing, newly returned element = previously rendered one. ? When they are not equal, React will update the DOM 44
  • 45. HOC ● A function that takes a component and returns a new component. ● Advanced technique in React for reusing component logic. ● Not part of the React API ● A pattern that emerges from React’s compositional nature. 45
  • 46. Pure Components ● React.PureComponent implements it with a shallow prop and state comparison and does not re-render if the they don’t change. ● So it handles shouldComponentUpdate() for you ● Does not re-render if nextState = prevState 46
  • 47. Memo ● React.memo is a higher order component. ● Similar to React.PureComponent but for functional based components instead of classes. ● If your function component renders the same result given the same props, you can wrap it in a call to React.memo 47
  • 48. Use of Refs ● React.createRef creates a reference that can be attached to React elements via the ref attribute. ● Avoid using jQuery as you have to import the jQuery library. as we’re going to manipulate the dom ● Compared to using jQuery, elements can be faster referenced in a React way using Refs ● Refs are not present during the initial render . You can initialize it in constructor. However you must use it inside one of the React’s lifecycle events such as componentDidMount or componentDidUpdate. 48
  • 49. 49
  • 50. Context ● Universal centralized data for your application. ● Used to share data, such as the current authenticated user, theme, or preferred language, between components. ● We can avoid passing props through intermediate elements 50
  • 52. 2-Wrap in Provider and pass the state to child component
  • 53. 3-Wrap in Consumer and receive context in Child Components
  • 54. Pros of Context ● Set context on top and all the components in the middle don’t have to know anything about it ● The one down at the bottom can access it ● Fills the same need as redux. 54
  • 55. Cons of Context ● Makes debugging difficult ● Difficult to figure out what caused the error when you cannot see the data in the child components that is not importing Context ● You have to look at all the components which are Consuming it to figure out which one of them caused the problem ● Use context only when you have to. 55
  • 56. Difference between Component state & Store state Component State =State is managed within a component - State value can change, and then render function is called. Store state =Store State is updated, by the reducer, when an action is triggered. 56
  • 57. 57