SlideShare uma empresa Scribd logo
1 de 23
React
Introduction to React
By Angela Lehru
@LehruAJay
What is React
A Javascript library for building user interfaces like web applications.
React can be used as a base in the development of single-page applications.
Create and Render a React Element
const element = React.createElement(
'h1',
{
id: 'main-title',
title: 'This is a title'
},
'Hello World'
)
ReactDOM.render(element, document.getElementById('root'));
React Dom
A library that lets React connect to and update the DOM
JSX
A syntax extension to Javascript that is used with React to describe elements in
the UI. As we’ll see, it resembles writing HTML.
We need Babel to transpile JSX into Javascript to be rendered on the browser. Try
it out babeljs.io/repl
To add Babel in a project:
<html/><head/> - <script src="https://unpkg.com/babel-
standalone@6.15.0/babel.min.js"></script>
<html/><head/> - <script type="text/babel" src="app.js"></script>
Set up a development server
Install http-server
1. npm init -y
2. npm i -S http-server
Add a start script to the package.json
"scripts": {
"start": "http-server"
},
Create React Project
create-react-app is an abstract way of getting started on a React Project. It
configures:
- Development server
- Webpack
- Babel
To install via command line/terminal:
npx create-react-app project-name
Let’s build an Inventory App
Components
Components
A typical functional component
import React, { Component, Fragment} from 'react';
const HelloWorld () {
return (
<h1>Hello World</h1>
);
}
export default HelloWorld;
State
A class component
import React, { Component, Fragment} from 'react';
class MainBody extends Component {
state = {
value:0
}
formatCount() {
return this.state.value === 0 ? 'Zero' : this.state.value
}
render() {
return (
<Fragment>
<span>{this.formatCount()}</span>
...
</Fragment>
);
}
}
export default MainBody;
Handling events
import React, { Component, Fragment} from 'react';
class MainBody extends Component {
state = {
count:0
}
handleIncrement () {
console.log(‘Add clicked’)
}
render() {
return (
<Fragment>
<button onClick={this.handleIncrement}>Add</button>
...
</Fragment>
);
}
export default MainBody;
Updating state
this.setState({ value: state})
import React, { Component, Fragment} from 'react';
class MainBody extends Component {
state = {
value:0
}
handleIncrement = () => {
console.log(‘Add clicked’, this.state.value)
}
render() {
return (
<Fragment>
<button onClick={this.handleIncrement}>Add</button>
...
</Fragment>
);
}
export default MainBody;
Parent - Child Components
import React, { Component, Fragment} from 'react';
class Items extends Component {
render() {
return (
<Fragment>
<Item />
<Item />
<Item />
</Fragment>
);
}
export default MainBody;
Rendering Lists
import React, { Component, Fragment} from 'react';
class MainBody extends Component {
state = {
tags: [tag1, tag2, tag3]
}
render() {
return (
<Fragment>
<ul>{this.state.tags.map(tag => <li key={tag}>{tag}</li>}</ul>
...
</Fragment>
);
}
export default MainBody;
Parent - Child Components
import React, { Component, Fragment} from 'react';
import MainBody from './components/mainBody';
class Items extends Component {
state = {
items: [
{ id: 1, value: 1 }
}
render() {
return (
<Fragment>
{this.state.items.map(item =>
<MainBody key={item.id} value={item.value}></MainBody>)}
</Fragment>
);
}
export default Items;
Passing Data to components(props)
import React, { Component, Fragment} from 'react';
class MainBody extends Component {
state = {
value:this.props.value
}
render() {
return (
<Fragment>
...
</Fragment>
);
}
export default Items;
Passing Children
Special prop called children, we use when we want to pass something between
the opening and closing tags of an element
1. Pass element in the component
2. Pass{this.props.children}in the parent component
<MainBody><h4>Item #{counter.id}</h4></MainBody>
React Tree
Props Vs State
Stateless Functional Components
Resources
Link to source code - Github repo
References used:
https://www.youtube.com/watch?v=Ke90Tje7VS0
https://reactjs.org/docs/
Questions

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Switch to React.js from AngularJS developer
Switch to React.js from AngularJS developerSwitch to React.js from AngularJS developer
Switch to React.js from AngularJS developer
 
React with Redux
React with ReduxReact with Redux
React with Redux
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 
React и redux
React и reduxReact и redux
React и redux
 
React & redux
React & reduxReact & redux
React & redux
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
Creating a WYSIWYG Editor with React
Creating a WYSIWYG Editor with ReactCreating a WYSIWYG Editor with React
Creating a WYSIWYG Editor with React
 
React redux
React reduxReact redux
React redux
 
React.js and Redux overview
React.js and Redux overviewReact.js and Redux overview
React.js and Redux overview
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
Introduction to React & Redux
Introduction to React & ReduxIntroduction to React & Redux
Introduction to React & Redux
 
React render props
React render propsReact render props
React render props
 
React and redux
React and reduxReact and redux
React and redux
 
Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2Quick start with React | DreamLab Academy #2
Quick start with React | DreamLab Academy #2
 
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max Petruck
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 

Semelhante a React outbox

Semelhante a React outbox (20)

React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
Getting Started with React v16
Getting Started with React v16Getting Started with React v16
Getting Started with React v16
 
Manage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's ReduxManage the Flux of your Web Application: Let's Redux
Manage the Flux of your Web Application: Let's Redux
 
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
 
Server side rendering with React and Symfony
Server side rendering with React and SymfonyServer side rendering with React and Symfony
Server side rendering with React and Symfony
 
Introduction to React for Frontend Developers
Introduction to React for Frontend DevelopersIntroduction to React for Frontend Developers
Introduction to React for Frontend Developers
 
Introduction to React and MobX
Introduction to React and MobXIntroduction to React and MobX
Introduction to React and MobX
 
Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017
 
ReactJS
ReactJSReactJS
ReactJS
 
React lecture
React lectureReact lecture
React lecture
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тяга
 
Intro react js
Intro react jsIntro react js
Intro react js
 
EWD 3 Training Course Part 38: Building a React.js application with QEWD, Part 4
EWD 3 Training Course Part 38: Building a React.js application with QEWD, Part 4EWD 3 Training Course Part 38: Building a React.js application with QEWD, Part 4
EWD 3 Training Course Part 38: Building a React.js application with QEWD, Part 4
 
React js
React jsReact js
React js
 
Intro to React | DreamLab Academy
Intro to React | DreamLab AcademyIntro to React | DreamLab Academy
Intro to React | DreamLab Academy
 
React 101
React 101React 101
React 101
 
Let's react - Meetup
Let's react - MeetupLet's react - Meetup
Let's react - Meetup
 
Stay with React.js in 2020
Stay with React.js in 2020Stay with React.js in 2020
Stay with React.js in 2020
 
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

React outbox

  • 1. React Introduction to React By Angela Lehru @LehruAJay
  • 2. What is React A Javascript library for building user interfaces like web applications. React can be used as a base in the development of single-page applications.
  • 3. Create and Render a React Element const element = React.createElement( 'h1', { id: 'main-title', title: 'This is a title' }, 'Hello World' ) ReactDOM.render(element, document.getElementById('root'));
  • 4. React Dom A library that lets React connect to and update the DOM
  • 5. JSX A syntax extension to Javascript that is used with React to describe elements in the UI. As we’ll see, it resembles writing HTML. We need Babel to transpile JSX into Javascript to be rendered on the browser. Try it out babeljs.io/repl To add Babel in a project: <html/><head/> - <script src="https://unpkg.com/babel- standalone@6.15.0/babel.min.js"></script> <html/><head/> - <script type="text/babel" src="app.js"></script>
  • 6. Set up a development server Install http-server 1. npm init -y 2. npm i -S http-server Add a start script to the package.json "scripts": { "start": "http-server" },
  • 7. Create React Project create-react-app is an abstract way of getting started on a React Project. It configures: - Development server - Webpack - Babel To install via command line/terminal: npx create-react-app project-name
  • 8. Let’s build an Inventory App
  • 10. Components A typical functional component import React, { Component, Fragment} from 'react'; const HelloWorld () { return ( <h1>Hello World</h1> ); } export default HelloWorld;
  • 11. State A class component import React, { Component, Fragment} from 'react'; class MainBody extends Component { state = { value:0 } formatCount() { return this.state.value === 0 ? 'Zero' : this.state.value } render() { return ( <Fragment> <span>{this.formatCount()}</span> ... </Fragment> ); } } export default MainBody;
  • 12. Handling events import React, { Component, Fragment} from 'react'; class MainBody extends Component { state = { count:0 } handleIncrement () { console.log(‘Add clicked’) } render() { return ( <Fragment> <button onClick={this.handleIncrement}>Add</button> ... </Fragment> ); } export default MainBody;
  • 13. Updating state this.setState({ value: state}) import React, { Component, Fragment} from 'react'; class MainBody extends Component { state = { value:0 } handleIncrement = () => { console.log(‘Add clicked’, this.state.value) } render() { return ( <Fragment> <button onClick={this.handleIncrement}>Add</button> ... </Fragment> ); } export default MainBody;
  • 14. Parent - Child Components import React, { Component, Fragment} from 'react'; class Items extends Component { render() { return ( <Fragment> <Item /> <Item /> <Item /> </Fragment> ); } export default MainBody;
  • 15. Rendering Lists import React, { Component, Fragment} from 'react'; class MainBody extends Component { state = { tags: [tag1, tag2, tag3] } render() { return ( <Fragment> <ul>{this.state.tags.map(tag => <li key={tag}>{tag}</li>}</ul> ... </Fragment> ); } export default MainBody;
  • 16. Parent - Child Components import React, { Component, Fragment} from 'react'; import MainBody from './components/mainBody'; class Items extends Component { state = { items: [ { id: 1, value: 1 } } render() { return ( <Fragment> {this.state.items.map(item => <MainBody key={item.id} value={item.value}></MainBody>)} </Fragment> ); } export default Items;
  • 17. Passing Data to components(props) import React, { Component, Fragment} from 'react'; class MainBody extends Component { state = { value:this.props.value } render() { return ( <Fragment> ... </Fragment> ); } export default Items;
  • 18. Passing Children Special prop called children, we use when we want to pass something between the opening and closing tags of an element 1. Pass element in the component 2. Pass{this.props.children}in the parent component <MainBody><h4>Item #{counter.id}</h4></MainBody>
  • 22. Resources Link to source code - Github repo References used: https://www.youtube.com/watch?v=Ke90Tje7VS0 https://reactjs.org/docs/

Notas do Editor

  1. A single-page application (SPA) is a web application or web site that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from a server. This approach avoids interruption of the user experience between successive pages.
  2. Add react to a website https://reactjs.org/docs/add-react-to-a-website.html
  3. The createElement() method creates and returns a React element of the given type React doesn’t create actual DOM nodes. React creates objects that describe DOM nodes The ReactDOM.render() method renders React elements to the DOM React doesn’t manipulate and update the DOM directly. React only manages what gets rendered to the DOM via ReactDOM.render(). It’s the job of render() to interpret the element objects and create DOM nodes out of them.
  4. Webpack for bundling our files Babel for compiling our JSX into javascript
  5. Create Navbar and render it in App.js
  6. Arrow functions -> to access this.state.value Update state -> this.setState({ value: this.state.value + 1 })
  7. {this.state.counters.map(counter => <MainBody key={counter.id} value={counter.value}> <h4>Item #{counter.id}</h4> </MainBody>)}