SlideShare a Scribd company logo
Introduction to React
A workshop for COMP 523
Aaron Smith
Monday, Feb. 10, 2020
What is React?
• React is a JavaScript framework
• Used for front end web development
• Think of jQuery, but more structured
• Created and used by Facebook
• Famous for implementing a virtual dom
Timeline of front-end JavaScript frameworks
jQuery*
(2006)
AngularJS
(2010)
React
(2013)
Vue
(2014)
Angular
(2014)
* jQuery is more often considered a library than a framework
Common tasks in front-end development
Data definition, organization, and storage
Event handlers respond to user actions
Design and render HTML templates
Resolve URLs
Interact with server(s) through APIs and AJAX
App state
User actions
Templates
Routing
Data fetching
Fundamentals of React
1. JavaScript and HTML in the same file (JSX)
2. Embrace functional programming
3. Components everywhere
JavaScript and HTML in the same file
Traditional
approach
React
approach
HTML CSS JS JSX CSS or JSS
JSX: the React programming language
const first = "Aaron";
const last = "Smith";
const name = <span>{first} {last}</span>;
const listWithTitle = (
<>
<h1>COMP 523</h1>
<ul>
<li>Dr. David Stotts</li>
<li>{name}</li>
</ul>
</>
);
const list = (
<ul>
<li>Dr. David Stotts</li>
<li>{name}</li>
</ul>
);
“React is just JavaScript”
Functional programming
1. Functions are “first class citizens”
2. Variables are immutable
3. Functions have no side effects
Functional programming
Functions are “first class citizens”
let add = function() {
console.log('Now adding numbers');
const five = 3 + 2;
};
function performTask(task) {
task();
console.log('Task performed!');
}
performTask(add);
function foo() {
return function() {
console.log('What gets printed?');
};
}
foo
foo();
foo()();
Functional programming
Variables are immutable
let a = 4;
a = 2; // Mutates `a`
let b = [1, 2, 3];
b.push(4); // Mutates `b`
let c = [...b, 4]; // Does not mutate `b`
Functional programming
Functions have no side effects
const b = [];
function hasSideEffects() {
b = [0];
}
Components
Components are functions for user interfaces
let y = f(x);
Input x Output number
let y = <FancyDiv value={x} />;
Input x Output HTML
Math function:
Component function:
Anatomy of a React component
export default function MyComponent(props) {
return <div>Hello, world! My name is {props.name}</div>;
}
const html = <MyComponent name="aaron" />;
Inputs are passed through a
single argument called “props”
The function is executed as if
it was an HTML tag
The function
outputs HTML
The component is just
a function
Parameters are passed in
as HTML attributes
Component rendering
• When a component function executes, we say it “renders”
• Assume components may re-render at any time
Our job is to ensure that
every time the component re-renders,
the correct output is produced
“In React, everything is a component”
Todo application
Title
TodoForm
First step:
 mockup / wireframe
TodoList
Todo
Big idea:
 A digital to-do list
Creating a new React app
Creating a new React app is simple!
1. Install Node.js
2. Run: npx create-react-app app-name
3. New app created in folder: ./app-name
Anatomy of a new React app
public holds the initial html
document and other static assets
App is a boilerplate
starter component
index.js binds
React to the DOM
package.json configures
npm dependencies
Component Hierarchy
Title
TodoForm
TodoList
Todo
Title TodoForm
TodoList
Todo Todo Todo
App
Special list key property
• Situation: Display a dynamic array of elements
• Must specify a special “key” property for each element
• The key of an item uniquely identifies it
• Used by React internally for render optimization
• Can be any unique value (string or number)
What are hooks?
useState
useEffect
useReducer
useMemo
useRef
useCallback
We will cover
these today
We will not cover
these today
Built-in hooks:
Hooks: Special functions that allow
developers to hook into state and
lifecycle of React components.
State: One or more data values
associated with a React component
instance.
Lifecycle: The events associated with a
React component instance (create,
render, destroy, etc).
First React hook: useState
Purpose:
1. Remember values internally when the component re-renders
2. Tell React to re-render the component when the value changes
Syntax:
const [val, setVal] = useState(100);
The current value A setter function to
change the value
The initial
value to use
Predicting component re-rendering
A component will only re-render when…
1. A value inside props changes
– or –
2. A useState setter is called
Second React hook: useEffect
Purpose:
Act as an observer, running code in response to value changes
Syntax:
useEffect(() => {
console.log(`myValue was changed! New value: ${myValue}`);
}, [myValue]);
A list of values such that changes
should trigger this code to run
The code to run when
values change
Building a React project
• When you’re ready to launch your app, run this command:
npm run build
• This bundles your app into CSS/JS/HTML files and puts them in the
/build folder
• These files can be used to deploy our projects
3rd party components and libraries
• React-Router
• Redux
• Material-UI
• Bootstrap
• Font-Awesome

More Related Content

What's hot

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
Piyush Jamwal
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
Knoldus Inc.
 
React workshop
React workshopReact workshop
React workshop
Imran Sayed
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
Varun Raj
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
Ignacio Martín
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
Imran Sayed
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
valuebound
 
React Hooks
React HooksReact Hooks
React Hooks
Erez Cohen
 
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
namespaceit
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
All Things Open
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
Divyang Bhambhani
 
React hooks
React hooksReact hooks
React hooks
Ramy ElBasyouni
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
Emanuele DelBono
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
ManojSatishKumar
 
Learn react-js
Learn react-jsLearn react-js
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
Knoldus Inc.
 
Reactjs
ReactjsReactjs
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
Arno Lordkronos
 

What's hot (20)

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 ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
React js
React jsReact js
React js
 
React workshop
React workshopReact workshop
React workshop
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux 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
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
React Hooks
React HooksReact Hooks
React Hooks
 
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
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 
React js - The Core Concepts
React js - The Core ConceptsReact js - The Core Concepts
React js - The Core Concepts
 
React hooks
React hooksReact hooks
React hooks
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
Reactjs
ReactjsReactjs
Reactjs
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 

Similar to react-slides.pptx

react-slides.pdf
react-slides.pdfreact-slides.pdf
react-slides.pdf
DayNightGaMiNg
 
react-slides.pdf gives information about react library
react-slides.pdf gives information about react libraryreact-slides.pdf gives information about react library
react-slides.pdf gives information about react library
janet736113
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
Nitin Tyagi
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
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
StephieJohn
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
Sebastian Pederiva
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
BOSC Tech Labs
 
React js
React jsReact js
React js
Rajesh Kolla
 
How to create components in react js
How to create components in react jsHow to create components in react js
How to create components in react js
BOSC Tech Labs
 
ReactJS
ReactJSReactJS
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
SamyakShetty2
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
Remo Jansen
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR3
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJS
Pratik Majumdar
 
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
Jeff Durta
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
BOSC Tech Labs
 

Similar to react-slides.pptx (20)

react-slides.pdf
react-slides.pdfreact-slides.pdf
react-slides.pdf
 
react-slides.pdf gives information about react library
react-slides.pdf gives information about react libraryreact-slides.pdf gives information about react library
react-slides.pdf gives information about react library
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
 
ReactJS (1)
ReactJS (1)ReactJS (1)
ReactJS (1)
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
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
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
React js
React jsReact js
React js
 
How to create components in react js
How to create components in react jsHow to create components in react js
How to create components in react js
 
ReactJS
ReactJSReactJS
ReactJS
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
REACT pdf.docx
REACT pdf.docxREACT pdf.docx
REACT pdf.docx
 
Intro react js
Intro react jsIntro react js
Intro react js
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJS
 
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
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 

react-slides.pptx

  • 1. Introduction to React A workshop for COMP 523 Aaron Smith Monday, Feb. 10, 2020
  • 2. What is React? • React is a JavaScript framework • Used for front end web development • Think of jQuery, but more structured • Created and used by Facebook • Famous for implementing a virtual dom
  • 3. Timeline of front-end JavaScript frameworks jQuery* (2006) AngularJS (2010) React (2013) Vue (2014) Angular (2014) * jQuery is more often considered a library than a framework
  • 4. Common tasks in front-end development Data definition, organization, and storage Event handlers respond to user actions Design and render HTML templates Resolve URLs Interact with server(s) through APIs and AJAX App state User actions Templates Routing Data fetching
  • 5. Fundamentals of React 1. JavaScript and HTML in the same file (JSX) 2. Embrace functional programming 3. Components everywhere
  • 6. JavaScript and HTML in the same file Traditional approach React approach HTML CSS JS JSX CSS or JSS
  • 7. JSX: the React programming language const first = "Aaron"; const last = "Smith"; const name = <span>{first} {last}</span>; const listWithTitle = ( <> <h1>COMP 523</h1> <ul> <li>Dr. David Stotts</li> <li>{name}</li> </ul> </> ); const list = ( <ul> <li>Dr. David Stotts</li> <li>{name}</li> </ul> );
  • 8. “React is just JavaScript”
  • 9. Functional programming 1. Functions are “first class citizens” 2. Variables are immutable 3. Functions have no side effects
  • 10. Functional programming Functions are “first class citizens” let add = function() { console.log('Now adding numbers'); const five = 3 + 2; }; function performTask(task) { task(); console.log('Task performed!'); } performTask(add); function foo() { return function() { console.log('What gets printed?'); }; } foo foo(); foo()();
  • 11. Functional programming Variables are immutable let a = 4; a = 2; // Mutates `a` let b = [1, 2, 3]; b.push(4); // Mutates `b` let c = [...b, 4]; // Does not mutate `b`
  • 12. Functional programming Functions have no side effects const b = []; function hasSideEffects() { b = [0]; }
  • 13. Components Components are functions for user interfaces let y = f(x); Input x Output number let y = <FancyDiv value={x} />; Input x Output HTML Math function: Component function:
  • 14. Anatomy of a React component export default function MyComponent(props) { return <div>Hello, world! My name is {props.name}</div>; } const html = <MyComponent name="aaron" />; Inputs are passed through a single argument called “props” The function is executed as if it was an HTML tag The function outputs HTML The component is just a function Parameters are passed in as HTML attributes
  • 15. Component rendering • When a component function executes, we say it “renders” • Assume components may re-render at any time Our job is to ensure that every time the component re-renders, the correct output is produced
  • 16. “In React, everything is a component”
  • 17. Todo application Title TodoForm First step:  mockup / wireframe TodoList Todo Big idea:  A digital to-do list
  • 18. Creating a new React app Creating a new React app is simple! 1. Install Node.js 2. Run: npx create-react-app app-name 3. New app created in folder: ./app-name
  • 19. Anatomy of a new React app public holds the initial html document and other static assets App is a boilerplate starter component index.js binds React to the DOM package.json configures npm dependencies
  • 21. Special list key property • Situation: Display a dynamic array of elements • Must specify a special “key” property for each element • The key of an item uniquely identifies it • Used by React internally for render optimization • Can be any unique value (string or number)
  • 22. What are hooks? useState useEffect useReducer useMemo useRef useCallback We will cover these today We will not cover these today Built-in hooks: Hooks: Special functions that allow developers to hook into state and lifecycle of React components. State: One or more data values associated with a React component instance. Lifecycle: The events associated with a React component instance (create, render, destroy, etc).
  • 23. First React hook: useState Purpose: 1. Remember values internally when the component re-renders 2. Tell React to re-render the component when the value changes Syntax: const [val, setVal] = useState(100); The current value A setter function to change the value The initial value to use
  • 24. Predicting component re-rendering A component will only re-render when… 1. A value inside props changes – or – 2. A useState setter is called
  • 25. Second React hook: useEffect Purpose: Act as an observer, running code in response to value changes Syntax: useEffect(() => { console.log(`myValue was changed! New value: ${myValue}`); }, [myValue]); A list of values such that changes should trigger this code to run The code to run when values change
  • 26. Building a React project • When you’re ready to launch your app, run this command: npm run build • This bundles your app into CSS/JS/HTML files and puts them in the /build folder • These files can be used to deploy our projects
  • 27. 3rd party components and libraries • React-Router • Redux • Material-UI • Bootstrap • Font-Awesome