SlideShare uma empresa Scribd logo
1 de 79
Baixar para ler offline
A full introductory

guide to React
Jean Emer
@jcemer

jcemer.com
Work&Co

Digital Products
***
1of9
What is
React for
React is a library for
building modular
and reusable user
interfaces
It is good for
webapps that has
different states and
also to define a
styleguide
State management
is the coordination
of actions and its
effects
http://material-ui.com
Styleguide is a
good way to keep
your project in
shape
2of9
What is a React
component
class HelloMessage extends React.Component {
render() {
return (
<div>
Hello {this.props.name}
</div>
);
}
}
JSX is XML with
JavaScript
class HelloMessage extends React.Component {
render() {
return React.createElement(
"div",
null,
"Hello ",
this.props.name
);
}
}
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
tick() {
this.setState(prevState => ({
seconds: prevState.seconds + 1
}));
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
<div>Seconds: {this.state.seconds}</div>
);
}
}
State
management
Life cycles
HTML with
states and props
A React component
is props, states, and
life cycle hooks
together
React is declarative,
there is no need to
define the interface
transitions
<div>Seconds: {this.state.seconds}</div>
I want a <div> with
the seconds inside.
Do it!
The role of the
Virtual DOM is to
transform the render
result in imperative
DOM instructions
<div>Seconds: 1</div>
From
<div>Seconds: 2</div>
To
element.textContent = 'Seconds 2'
Do
return (
<ReactCSSTransitionGroup
transitionName='number'
transitionEnterTimeout={200}
transitionLeaveTimeout={200}>
<div key={this.state.seconds}>
Seconds: {this.state.seconds}
</div>
</ReactCSSTransitionGroup>
);
https://reactjs.org/docs/animation.html
3of9
How to setup 

and build 

React
ReactDOM.render(
<Component any={data} />,
document

.getElementById(‘component-root')
);
It renders a
component in
the document
There is at least the
need of using Babel
with Transform
React JSX plugin
https://github.com/
facebookincubator/create-react-app
https://github.com/facebookincubator/create-react-app/blob/
master/packages/react-scripts/config/webpack.config.dev.js
4of9
How to fetch
data for a
component
class ServerTime extends React.Component {
constructor(props) {
super(props);
this.state = { time: 'Fetching' };
}
componentDidMount() {
axios.get('/time.json')
.then(res => this.setState({ time: res.data.time }));
}
render() {
return (
<div>{this.state.time}</div>
);
}
}
Get a response
and change
the state
Fetch the data
right before the
component is
mounted
But what happens
if the component
isn’t mounted
anymore when the
response arrives?
class ServerTime extends React.Component {
constructor(props) {
super(props);
this.cancelToken = axios.CancelToken.source();
this.state = { time: 'Fetching' };
}
componentDidMount() {
axios.get('/time.json', { cancelToken: this.cancelToken.token })
.then(res => this.setState({ time: res.data.time }));
}
componentWillUnmount() {
this.cancelToken.cancel('Operation canceled by the user.');
}
render() {
return (
<div>{this.state.time}</div>
);
}
}
Cancel token
https://discuss.reactjs.org/t/cancel-async-fetch-on-unmount/3272/2
5of9
How to share
state between
components
class ServerTime extends React.Component {
constructor(props) {
super(props);
this.state = { time: 'Fetching' };
}
componentDidMount() {
/* Fetch server time */
}
render() {
return (
<div>
<Header time={this.state.time} />
<Content time={this.state.time} />
<Footer time={this.state.time} />
</div>
);
}
}
Send the time via
prop to different
components
User
User
Transporting a
user through all the
component tree is a
boring job
window.user = { name: 'Jean' }
All components can
easily access the user
But what happens 

if the user login or
logout
https://redux.js.org/
The state store
keeps the user and
can be connected to
components
import { connect } from 'react-redux';
const mapStateToProps = state => ({ user: state.user })
export default connect(mapStateToProps)(Header);
Maps the user
to a prop
Login and logout
are actions that
change the state
store
store.dispatch(fetchUserAction);
this.props.dispatch(logoutAction);
On app bootstrap
Inside a connected
component
https://daveceddia.com/how-does-redux-work/
6of9
What is children,
HOC, render
prop, and more
All that will see 

are techniques to 

code reuse
function Content(props) {
return (
<div className='content'>
{props.children}
</div>
);
}
<Content>
<Article />
<Comments />
</Content>
Usage
Content is a
generic box
Higher-Order
Component is a
function that takes a
component and returns
a new component
React documentation
import { connect } from 'react-redux';
const mapStateToProps = state => ({ user: state.user })
export default connect(mapStateToProps)(Header);
It’s a HOC
<UserProvider>
{user => (
<Header {...this.props} user={user}/>

)}

</UserProvider>
Function as child
component
function UserProvider(props) {
return props.children(window.user);
}
A render prop is a
function prop that a
component uses to
know what to render
https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce
<UserProvider render={user => (
<Header {...this.props} user={user}/>
)}/>
Render is a prop
that accepts a
function to render
• Children
• Function as Child
Component
• Higher-Order
Component
• Function (render) as
Prop Component
const HelloWorld = () => (<div>Hello world</div>);
React Stateless
Functional
Components
7of9
A bit about
performance
React is fast!
https://reactjs.org/docs/react-api.html#reactpurecomponent
class HelloMessage extends React.Component {
render() {
return (
<div>
Hello {this.props.name}
</div>
);
}
}
class HelloMessage extends React.PureComponent {
render() {
return (
<div>
Hello {this.props.name}
</div>
);
}
}
Shallow
compare
shouldComponentUpdate()
8of9
How to route
an application
const BasicExample = () => (
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/topics">Topics</Link></li>
</ul>
<hr/>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/topics" component={Topics}/>
</div>
</Router>
)
React Router code example
It can be very
complex if you try
to async load data
and animate the
transitions
***
9of9
How to run
React on the
server
ReactDOM.renderToString(
<Component any={data} />

);
React Router default
routing is dynamic
and it is hard to fetch
data on the server
because of that
https://crypt.codemancers.com/posts/2017-06-03-reactjs-server-side-
rendering-with-router-v4-and-redux/
https://crypt.codemancers.com/posts/2017-06-03-reactjs-server-side-
rendering-with-router-v4-and-redux/
https://zeit.co/blog/next
https://zeit.co/blog/next
***
I hope it inspires you
@jcemer
Thank you!

Mais conteúdo relacionado

Mais procurados

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

Mais procurados (20)

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 
Struts tutorial
Struts tutorialStruts tutorial
Struts tutorial
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
Academy PRO: React JS
Academy PRO: React JSAcademy PRO: React JS
Academy PRO: React JS
 
Cleaning your architecture with android architecture components
Cleaning your architecture with android architecture componentsCleaning your architecture with android architecture components
Cleaning your architecture with android architecture components
 
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
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
React, Flux and a little bit of Redux
React, Flux and a little bit of ReduxReact, Flux and a little bit of Redux
React, Flux and a little bit of Redux
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React & redux
React & reduxReact & redux
React & redux
 
Redux training
Redux trainingRedux training
Redux training
 
React and redux
React and reduxReact and redux
React and redux
 
React JS part 1
React JS part 1React JS part 1
React JS part 1
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 

Semelhante a A full introductory guide to React

Semelhante a A full introductory guide to React (20)

ReactJS
ReactJSReactJS
ReactJS
 
Intro react js
Intro react jsIntro react js
Intro react js
 
Introduction to React for Frontend Developers
Introduction to React for Frontend DevelopersIntroduction to React for Frontend Developers
Introduction to React for Frontend Developers
 
React & The Art of Managing Complexity
React &  The Art of Managing ComplexityReact &  The Art of Managing Complexity
React & The Art of Managing Complexity
 
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/Redux
React/ReduxReact/Redux
React/Redux
 
Let's react - Meetup
Let's react - MeetupLet's react - Meetup
Let's react - Meetup
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
 
React - An Introduction
React - An IntroductionReact - An Introduction
React - An Introduction
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectReact for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence Connect
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 
react-en.pdf
react-en.pdfreact-en.pdf
react-en.pdf
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
OttawaJS - React
OttawaJS - ReactOttawaJS - React
OttawaJS - React
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
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...
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+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@
 
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
 

Último (20)

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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
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...
 
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
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
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
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
+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...
 
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 ...
 
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
 

A full introductory guide to React