SlideShare une entreprise Scribd logo
1  sur  17
React presentation
React hooks vs Class-based components
Presented By:
Lehru Angela Jamimah
Class-based and Functional Components
Class-based components -> Used when you need to define state, lifecycle
methods.
Functional components -> Cannot define state or lifecycle methods within the
component.
Let’s build a React app with classes
STEP 1.
1. Prerequisites
All we need is Node and React 16.8.0 or higher.
$ npm install --save react@^16.8.0 react-dom@^16.8.0
1. Create an application
$ npx create-react-app my-app
$ cd my-app
$ npm start
Let’s build a React app with classes
STEP 2.
1. Create some class based and functional components.
// Todo.js (class based component)
import React, {Component} from 'react';
import Item from './Item';
class Todo extends Component {
state = {
todos: [
{text: 'Learn about React'},
{text: 'Meet friend for lunch'},
{text: 'Build really cool todo app'}
]
};
Let’s build a React app with classes (Continued)
handleShowTodos = () => {
const { showTodos } = this.state;
this.setState({ showTodos: !showTodos });
};
render(){
const {todos} = this.state;
return(
<div className='app'>
<div className='todo-list'>
<button onClick={this.handleShowTodos}>Show Todos</button>
{todos.map((todo, index)=>(
<Item key={index} index={index} todo={todo} />
))}
</div>
</div> )}}
export default Todo;
Let’s build a React app with classes (Continued)
// Item.js (functional component)
import React from 'react';
const Item = props => {
const {todo} = props;
return(
<div className="item">
{todo.text}
</div>
)
}
export default Item;
Let’s build a React app with classes
STEP 3.
1. Application works, great!
React hooks
What are React hooks?
Hooks are functions that let you use state and lifecycle method features in
functional components. They let you use React without classes.
Allows you to create every component as a functional component which is clean
and simple.
Classes vs react hooks/ why use React hooks
● Complex components become hard to understand
● Classes confuse both people and machines
Note:
● 100% backwards-compatible
● There are no plans to remove classes from React
● Hooks don’t replace your knowledge of React concepts
Syntax differences between classes and react
hooks
● useState(...) replaces state and setState(...).
● useEffect replaces lifecycle methods.
● Functions replace classes.
Let’s introduce React hooks into our application
STEP 1.
1. Use the useState hook.
// Todo.js
. . .
const Todo = props => {
const [todos, setTodos] = useState([
{text: 'Learn about React'},
{text: 'Meet friend for lunch'},
{text: 'Build really cool todo app'}
]);
const [showTodos, setShowTodos] = useState(false);
const handleShowTodos = () => {
setShowTodos(!showTodos);
};
Let’s introduce React hooks into our application
return (
<div className="app">
<div className="todo-list">
<button onClick={handleShowTodos}>Show Todos</button>
{showTodos && todos.map((todo, index)=>(
<Item key={index} index={index} todo={todo} />
))}
</div>
</div>
)
}
export default Todo;
Let’s introduce React hooks into our application
STEP 2
1. Add a lifecycle method
state = {
. . .
title: ''
};
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => this.setState({title: json.title}))
};
render(){
return(
<p>{title}</p>
. . .
Let’s introduce React hooks into our application
STEP 2
2. Use the useEffect hook.
. . .
const [title, setTitle] = useState('');
useEffect(()=>{
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => setTitle(json.title))
}, []);
return (
. . .
<p>{title}</p>
)
Let’s introduce React hooks into our application
STEP 2.
1. Application works, great!
Homework
Look into how the useEffect hook is used to replace the rest of the lifecycle
methods i.e componentDidUpdate(), componentWillUnmount(),
shouldComponentUpdate() and getDerivedStateFromProps().
Resources
Github repo - https://github.com/LehruAngela/react-hooks
Docs and tutorials:
● https://scotch.io/tutorials/build-a-react-to-do-app-with-react-hooks-no-class-components
● https://reactjs.org/docs/hooks-intro.html
● https://www.youtube.com/watch?v=-MlNBTSg_Ww&t=2687s
Pluginkla2019 - React Presentation

Contenu connexe

Tendances

Tendances (20)

React Hooks
React HooksReact Hooks
React Hooks
 
Potato04 The end of confusion of callback between activity and fragment.
Potato04 The end of confusion of callback between activity and fragment.Potato04 The end of confusion of callback between activity and fragment.
Potato04 The end of confusion of callback between activity and fragment.
 
Building Universal Applications with Angular 2
Building Universal Applications with Angular 2Building Universal Applications with Angular 2
Building Universal Applications with Angular 2
 
Building user interface with react
Building user interface with reactBuilding user interface with react
Building user interface with react
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooks
 
3 Simple Steps to follow to Create React JS Components
3 Simple Steps to follow to Create React JS Components3 Simple Steps to follow to Create React JS Components
3 Simple Steps to follow to Create React JS Components
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
JSAnkara Swift v React Native
JSAnkara Swift v React NativeJSAnkara Swift v React Native
JSAnkara Swift v React Native
 
Angular 1.x vs 2 - In code level
Angular 1.x vs 2 - In code levelAngular 1.x vs 2 - In code level
Angular 1.x vs 2 - In code level
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2
 
Tools & tricks for testing React applications
Tools & tricks for testing React applications Tools & tricks for testing React applications
Tools & tricks for testing React applications
 
Dagger2
Dagger2Dagger2
Dagger2
 
Redux Saga - Under the hood
Redux Saga - Under the hoodRedux Saga - Under the hood
Redux Saga - Under the hood
 
React JS part 1
React JS part 1React JS part 1
React JS part 1
 
How to push a react js application in production and sleep better
How to push a react js application in production and sleep betterHow to push a react js application in production and sleep better
How to push a react js application in production and sleep better
 
Angular js 2.0
Angular js 2.0Angular js 2.0
Angular js 2.0
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
20190619 meetup r_shiny_reactlog_v0.1
20190619 meetup r_shiny_reactlog_v0.120190619 meetup r_shiny_reactlog_v0.1
20190619 meetup r_shiny_reactlog_v0.1
 
7 Redux challenges
7 Redux challenges7 Redux challenges
7 Redux challenges
 

Similaire à Pluginkla2019 - React Presentation

Similaire à Pluginkla2019 - React Presentation (20)

Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
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 Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
Simple React Todo List
Simple React Todo ListSimple React Todo List
Simple React Todo List
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
How To Integrate Native Android App With React Native.
How To Integrate Native Android App With React Native.How To Integrate Native Android App With React Native.
How To Integrate Native Android App With React Native.
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
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
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
 
Intro react js
Intro react jsIntro react js
Intro react js
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
Green Custard Friday Talk 21: React Hooks
Green Custard Friday Talk 21: React HooksGreen Custard Friday Talk 21: React Hooks
Green Custard Friday Talk 21: React Hooks
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Introduction to react_js
Introduction to react_jsIntroduction to react_js
Introduction to react_js
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to React
 
React state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of HooksReact state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of Hooks
 
Lec7Handout.ppt
Lec7Handout.pptLec7Handout.ppt
Lec7Handout.ppt
 
Owl: The New Odoo UI Framework
Owl: The New Odoo UI FrameworkOwl: The New Odoo UI Framework
Owl: The New Odoo UI Framework
 
react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 

Dernier

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Dernier (20)

Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
%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
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%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
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
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 🔝✔️✔️
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
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
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%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
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

Pluginkla2019 - React Presentation

  • 1. React presentation React hooks vs Class-based components Presented By: Lehru Angela Jamimah
  • 2. Class-based and Functional Components Class-based components -> Used when you need to define state, lifecycle methods. Functional components -> Cannot define state or lifecycle methods within the component.
  • 3. Let’s build a React app with classes STEP 1. 1. Prerequisites All we need is Node and React 16.8.0 or higher. $ npm install --save react@^16.8.0 react-dom@^16.8.0 1. Create an application $ npx create-react-app my-app $ cd my-app $ npm start
  • 4. Let’s build a React app with classes STEP 2. 1. Create some class based and functional components. // Todo.js (class based component) import React, {Component} from 'react'; import Item from './Item'; class Todo extends Component { state = { todos: [ {text: 'Learn about React'}, {text: 'Meet friend for lunch'}, {text: 'Build really cool todo app'} ] };
  • 5. Let’s build a React app with classes (Continued) handleShowTodos = () => { const { showTodos } = this.state; this.setState({ showTodos: !showTodos }); }; render(){ const {todos} = this.state; return( <div className='app'> <div className='todo-list'> <button onClick={this.handleShowTodos}>Show Todos</button> {todos.map((todo, index)=>( <Item key={index} index={index} todo={todo} /> ))} </div> </div> )}} export default Todo;
  • 6. Let’s build a React app with classes (Continued) // Item.js (functional component) import React from 'react'; const Item = props => { const {todo} = props; return( <div className="item"> {todo.text} </div> ) } export default Item;
  • 7. Let’s build a React app with classes STEP 3. 1. Application works, great!
  • 8. React hooks What are React hooks? Hooks are functions that let you use state and lifecycle method features in functional components. They let you use React without classes. Allows you to create every component as a functional component which is clean and simple.
  • 9. Classes vs react hooks/ why use React hooks ● Complex components become hard to understand ● Classes confuse both people and machines Note: ● 100% backwards-compatible ● There are no plans to remove classes from React ● Hooks don’t replace your knowledge of React concepts
  • 10. Syntax differences between classes and react hooks ● useState(...) replaces state and setState(...). ● useEffect replaces lifecycle methods. ● Functions replace classes.
  • 11. Let’s introduce React hooks into our application STEP 1. 1. Use the useState hook. // Todo.js . . . const Todo = props => { const [todos, setTodos] = useState([ {text: 'Learn about React'}, {text: 'Meet friend for lunch'}, {text: 'Build really cool todo app'} ]); const [showTodos, setShowTodos] = useState(false); const handleShowTodos = () => { setShowTodos(!showTodos); };
  • 12. Let’s introduce React hooks into our application return ( <div className="app"> <div className="todo-list"> <button onClick={handleShowTodos}>Show Todos</button> {showTodos && todos.map((todo, index)=>( <Item key={index} index={index} todo={todo} /> ))} </div> </div> ) } export default Todo;
  • 13. Let’s introduce React hooks into our application STEP 2 1. Add a lifecycle method state = { . . . title: '' }; componentDidMount() { fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => this.setState({title: json.title})) }; render(){ return( <p>{title}</p> . . .
  • 14. Let’s introduce React hooks into our application STEP 2 2. Use the useEffect hook. . . . const [title, setTitle] = useState(''); useEffect(()=>{ fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => setTitle(json.title)) }, []); return ( . . . <p>{title}</p> )
  • 15. Let’s introduce React hooks into our application STEP 2. 1. Application works, great! Homework Look into how the useEffect hook is used to replace the rest of the lifecycle methods i.e componentDidUpdate(), componentWillUnmount(), shouldComponentUpdate() and getDerivedStateFromProps().
  • 16. Resources Github repo - https://github.com/LehruAngela/react-hooks Docs and tutorials: ● https://scotch.io/tutorials/build-a-react-to-do-app-with-react-hooks-no-class-components ● https://reactjs.org/docs/hooks-intro.html ● https://www.youtube.com/watch?v=-MlNBTSg_Ww&t=2687s