SlideShare une entreprise Scribd logo
1  sur  51
Télécharger pour lire hors ligne
LEARNING REACTFACEBOOK'S JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES
MY NAME IS KEN WHEELER
I do amazing things at Media Hive in Red Bank, NJ
&
Build a bunch of open source stuff in my free time
"2014 = Angular, 2015 = React"
— Todd Motto
What is React?
▸ Just The View Layer
▸ Component Based
▸ Virtual DOM
▸ Unidirectional Data Flow
▸ Server Side Rendering
WHY REACT?
▸ Low Learning Curve & Small API
▸ Virtual DOM = Performance
▸ SEO problem solved
▸ Statefulness
▸ Standalone
What is the Virtual DOM?
// JSX
render: function(){
return (
<div className=’test’>Hello</div>
)
}
//JS
render: function(){
return (
React.DOM.div({className: ‘test’}, ‘hello’)
)
}
COMPONENTS
COMPONENTS
var MyComponent = React.createClass({
render: function(){
return (
<h1>Hello World</h1>
)
}
});
COMPONENTS
var MyComponent = React.createClass({
render: function(){
return (
<h1>Hello World</h1>
)
}
});
React.render(<MyComponent/>, document.body);
COMPONENTS
var OtherComponent = React.createClass({
render: function(){
return (
<h1>Hello World</h1>
)
}
})
var MyComponent = React.createClass({
render: function(){
return (
<div className="container">
<OtherComponent/>
</div>
)
}
});
React.render(<MyComponent/>, document.body);
JSX
JSX GOTCHAS
<div className="myClass"></div>
<label htmlFor="myInput"></label>
render: function() {
return (
<div>
<FirstComponent/>
<AdjacentComponent/>
</div>
)
}
PROPS
PROPS
var MyComponent = React.createClass({
render: function(){
return (
<h1>{this.props.message}</h1>
)
}
});
React.render(<MyComponent message="Hello World"/>, document.body);
STATE
STATE
var MyComponent = React.createClass({
getInitialState: function() {
return {
message: "Hello World"
}
},
render: function(){
return (
<h1>{this.state.message}</h1>
)
}
});
React.render(<MyComponent/>, document.body);
SPECIFICATIONS
▸ render
▸ getInitialState
▸ getDefaultProps
▸ propTypes
▸ mixins
▸ statics
▸ displayName
LIFECYCLE METHODS
▸ componentWillMount
▸ componentDidMount
▸ componentWillReceiveProps
▸ shouldComponentUpdate
▸ componentWillUpdate
▸ componentDidUpdate
▸ componentWillUnmount
KEYS
KEYS
var MyComponent = React.createClass({
getInitialState: function() {
return {
todos: ["Listen to Tupac", "Don't eat bread", "Put up Christmas decorations"]
}
},
render: function(){
var items = this.state.todos.map(function(todo){
return <li>{todo}</li>
})
return (
<ul>
{items}
</ul>
)
}
});
React.render(<MyComponent/>, document.body);
KEYS
var MyComponent = React.createClass({
getInitialState: function() {
return {
todos: ["Listen to Tupac", "Don't eat bread", "Put up Christmas decorations"]
}
},
render: function(){
var items = this.state.todos.map(function(todo, index){
return <li key={index}>{todo}</li>
})
return (
<ul>
{items}
</ul>
)
}
});
React.render(<MyComponent/>, document.body);
REFS
REFS
var MyComponent = React.createClass({
handleSubmit: function(){
console.log(this.refs.nameInput.value)
},
render: function(){
return (
<form onSubmit={this.handleSubmit}>
<label htmlFor="nameInput">Enter your name:</label>
<input type="text" name="nameInput" ref="nameInput"/>
</form>
)
}
});
React.render(<MyComponent/>, document.body);
EVENTS
EVENTS
var MyComponent = React.createClass({
handleClick: function(){
alert('I Got Clicked!')
},
render: function(){
return (
<a href="#" onClick={this.handleClick}/>Click Me</a>
)
}
});
React.render(<MyComponent/>, document.body);
SERVER SIDE REACT
SERVER SIDE REACT
// server.js
var state = {todos: ["Get in a fistfight", "Buy chain wallet"]}
var markup = React.renderToString(<MyComponent/>);
res.render('index',{
markup: markup,
state: state
});
SERVER SIDE REACT
// index.handlebars
<html>
<head></head>
<body>
{{{ markup }}}
<script id="app-state" type="application/json">{{{state}}}</script>
</body>
</html>
SERVER SIDE REACT
// client.js
var state = JSON.parse(document.getElementById('app-state').innerHTML);
React.render(<MyComponent state={state}/>, document.body);
FLUX ARCHITECTURE
What Is Flux Architecture?
FLUX ARCHITECTURE
▸ Global Pub/Sub System
▸ Actions send data and action type to Dispatcher
▸ Dispatcher sends actions to Stores & enforces dependencies
▸ Stores receive data, update their data and notify Views
▸ Views update & call actions
Why Flux?
THIS SUCKS
var ChildComponent = React.createClass({
render: function(){
<a href="#" onClick={this.props.onClick}/>
}
})
var TopComponent = React.createClass({
handleClick: function(){
console.log('meh');
},
render: function(){
return (
<ChildComponent onClick={this.handleClick}/>
)
}
})
FLUX ARCHITECTURE
FLUX ARCHITECTURE
FLUX ARCHITECTURE
Flux Architecture Using McFly
http://kenwheeler.github.io/mcfly
DISPATCHER
// Dispatcher.js
// Creates a new dispatcher and attaches it to mcFly.dispatcher
var mcFly = new McFly();
module.exports = mcFly;
ACTIONS
// TodoActions.js
var TodoActions = mcFly.createActions({
addTodo: function(todo){
return {
actionType: 'ADD_TODO',
todo: todo
}
}
})
module.exports = TodoActions;
STORES
//TodoStore.js
var _todos = [];
function addTodo(todo){
_todos = _todos.push(todo);
}
var TodoStore = mcFly.createStore({
getTodos: function(){
return _todos;
}
}, function(payload){
if(payload.actionType === "ADD_TODO") {
addTodo(payload.todo);
TodoStore.emitChange();
}
})
module.exports = TodoStore;
VIEWS
var TodoList = React.createClass({
mixins: ['TodoStore.mixin'],
getInitialState: function(){
return {
todos: TodoStore.getTodos()
}
},
addTodo: function(){
TodoActions.addTodo(this.refs.todoInput.value);
},
render: function(){
var todos = this.state.todos.map(function(todo,index){
return <li key={index}>{todo}</li>
})
return (
<div className="todos-app">
<ul className="todos">
{todos}
</ul>
<input type="text" ref="todoInput"/>
<input type="submit" onClick={this.addTodo} value="Add Todo"/>
</div>
)
}
})
THANK YOU!

Contenu connexe

Tendances

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 developerEugene Zharkov
 
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max PetruckMaksym Petruk
 
React for Dummies
React for DummiesReact for Dummies
React for DummiesMitch Chen
 
React.js or why DOM finally makes sense
React.js or why DOM finally makes senseReact.js or why DOM finally makes sense
React.js or why DOM finally makes senseEldar Djafarov
 
Accessible Ajax on Rails
Accessible Ajax on RailsAccessible Ajax on Rails
Accessible Ajax on Railssupervillain
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJSAll Things Open
 
Getting started with ReactJS
Getting started with ReactJSGetting started with ReactJS
Getting started with ReactJSKrishna Sunuwar
 
Up and Running with ReactJS
Up and Running with ReactJSUp and Running with ReactJS
Up and Running with ReactJSLoc Nguyen
 
JavaScript Testing for Rubyists
JavaScript Testing for RubyistsJavaScript Testing for Rubyists
JavaScript Testing for RubyistsJamie Dyer
 
A To-do Web App on Google App Engine
A To-do Web App on Google App EngineA To-do Web App on Google App Engine
A To-do Web App on Google App EngineMichael Parker
 
REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptDeepu S Nath
 
Kakunin E2E framework showcase
Kakunin E2E framework showcaseKakunin E2E framework showcase
Kakunin E2E framework showcaseThe Software House
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Eventsdmethvin
 

Tendances (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, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max Petruck
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
React.js or why DOM finally makes sense
React.js or why DOM finally makes senseReact.js or why DOM finally makes sense
React.js or why DOM finally makes sense
 
Accessible Ajax on Rails
Accessible Ajax on RailsAccessible Ajax on Rails
Accessible Ajax on Rails
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
An Introduction to ReactJS
An Introduction to ReactJSAn Introduction to ReactJS
An Introduction to ReactJS
 
ReactJS
ReactJSReactJS
ReactJS
 
Getting started with ReactJS
Getting started with ReactJSGetting started with ReactJS
Getting started with ReactJS
 
Up and Running with ReactJS
Up and Running with ReactJSUp and Running with ReactJS
Up and Running with ReactJS
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
JavaScript Testing for Rubyists
JavaScript Testing for RubyistsJavaScript Testing for Rubyists
JavaScript Testing for Rubyists
 
Getting Started With ReactJS
Getting Started With ReactJSGetting Started With ReactJS
Getting Started With ReactJS
 
A To-do Web App on Google App Engine
A To-do Web App on Google App EngineA To-do Web App on Google App Engine
A To-do Web App on Google App Engine
 
React outbox
React outboxReact outbox
React outbox
 
REACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScriptREACT.JS : Rethinking UI Development Using JavaScript
REACT.JS : Rethinking UI Development Using JavaScript
 
Kakunin E2E framework showcase
Kakunin E2E framework showcaseKakunin E2E framework showcase
Kakunin E2E framework showcase
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
jQuery 1.7 Events
jQuery 1.7 EventsjQuery 1.7 Events
jQuery 1.7 Events
 

Similaire à Learning React: Facebook's Javascript Library For Building User Interfaces

Introduction to React and MobX
Introduction to React and MobXIntroduction to React and MobX
Introduction to React and MobXAnjali Chawla
 
React.js workshop by tech47.in
React.js workshop by tech47.inReact.js workshop by tech47.in
React.js workshop by tech47.inJaikant Kumaran
 
Academy PRO: React JS
Academy PRO: React JSAcademy PRO: React JS
Academy PRO: React JSBinary Studio
 
Connect.js - Exploring React.Native
Connect.js - Exploring React.NativeConnect.js - Exploring React.Native
Connect.js - Exploring React.Nativejoshcjensen
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactJonne Kats
 
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 ReduxCommit University
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs[T]echdencias
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creatorsGeorge Bukhanov
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тягаVitebsk Miniq
 
Time to React!
Time to React!Time to React!
Time to React!STX Next
 
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...Luciano Mammino
 
Introduction to React for Frontend Developers
Introduction to React for Frontend DevelopersIntroduction to React for Frontend Developers
Introduction to React for Frontend DevelopersSergio Nakamura
 
Full Stack Toronto - the 3R Stack
Full Stack Toronto - the 3R StackFull Stack Toronto - the 3R Stack
Full Stack Toronto - the 3R StackScott Persinger
 

Similaire à Learning React: Facebook's Javascript Library For Building User Interfaces (20)

Introduction to React and MobX
Introduction to React and MobXIntroduction to React and MobX
Introduction to React and MobX
 
React 101
React 101React 101
React 101
 
React.js workshop by tech47.in
React.js workshop by tech47.inReact.js workshop by tech47.in
React.js workshop by tech47.in
 
Academy PRO: React JS
Academy PRO: React JSAcademy PRO: React JS
Academy PRO: React JS
 
React & Redux
React & ReduxReact & Redux
React & Redux
 
Connect.js - Exploring React.Native
Connect.js - Exploring React.NativeConnect.js - Exploring React.Native
Connect.js - Exploring React.Native
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and React
 
React lecture
React lectureReact lecture
React lecture
 
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
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creators
 
Reactивная тяга
Reactивная тягаReactивная тяга
Reactивная тяга
 
Time to React!
Time to React!Time to React!
Time to React!
 
React redux
React reduxReact redux
React redux
 
React js
React jsReact js
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...
 
Introduction to React for Frontend Developers
Introduction to React for Frontend DevelopersIntroduction to React for Frontend Developers
Introduction to React for Frontend Developers
 
React/Redux
React/ReduxReact/Redux
React/Redux
 
ReactJS.ppt
ReactJS.pptReactJS.ppt
ReactJS.ppt
 
Full Stack Toronto - the 3R Stack
Full Stack Toronto - the 3R StackFull Stack Toronto - the 3R Stack
Full Stack Toronto - the 3R Stack
 

Dernier

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...apidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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 educationjfdjdjcjdnsjd
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
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, ...apidays
 
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.pptxRustici Software
 
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 Pakistandanishmna97
 
"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 ...Zilliz
 
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 2024Victor Rentea
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
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 WoodJuan lago vázquez
 
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, Adobeapidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Dernier (20)

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...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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, ...
 
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
 
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
 
"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 ...
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Learning React: Facebook's Javascript Library For Building User Interfaces

  • 1. LEARNING REACTFACEBOOK'S JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES
  • 2. MY NAME IS KEN WHEELER
  • 3. I do amazing things at Media Hive in Red Bank, NJ & Build a bunch of open source stuff in my free time
  • 4. "2014 = Angular, 2015 = React" — Todd Motto
  • 6. ▸ Just The View Layer ▸ Component Based ▸ Virtual DOM ▸ Unidirectional Data Flow ▸ Server Side Rendering
  • 8. ▸ Low Learning Curve & Small API ▸ Virtual DOM = Performance ▸ SEO problem solved ▸ Statefulness ▸ Standalone
  • 9. What is the Virtual DOM?
  • 10. // JSX render: function(){ return ( <div className=’test’>Hello</div> ) } //JS render: function(){ return ( React.DOM.div({className: ‘test’}, ‘hello’) ) }
  • 11.
  • 12.
  • 14. COMPONENTS var MyComponent = React.createClass({ render: function(){ return ( <h1>Hello World</h1> ) } });
  • 15. COMPONENTS var MyComponent = React.createClass({ render: function(){ return ( <h1>Hello World</h1> ) } }); React.render(<MyComponent/>, document.body);
  • 16. COMPONENTS var OtherComponent = React.createClass({ render: function(){ return ( <h1>Hello World</h1> ) } }) var MyComponent = React.createClass({ render: function(){ return ( <div className="container"> <OtherComponent/> </div> ) } }); React.render(<MyComponent/>, document.body);
  • 17. JSX
  • 18. JSX GOTCHAS <div className="myClass"></div> <label htmlFor="myInput"></label> render: function() { return ( <div> <FirstComponent/> <AdjacentComponent/> </div> ) }
  • 19. PROPS
  • 20. PROPS var MyComponent = React.createClass({ render: function(){ return ( <h1>{this.props.message}</h1> ) } }); React.render(<MyComponent message="Hello World"/>, document.body);
  • 21. STATE
  • 22. STATE var MyComponent = React.createClass({ getInitialState: function() { return { message: "Hello World" } }, render: function(){ return ( <h1>{this.state.message}</h1> ) } }); React.render(<MyComponent/>, document.body);
  • 24. ▸ render ▸ getInitialState ▸ getDefaultProps ▸ propTypes ▸ mixins ▸ statics ▸ displayName
  • 26. ▸ componentWillMount ▸ componentDidMount ▸ componentWillReceiveProps ▸ shouldComponentUpdate ▸ componentWillUpdate ▸ componentDidUpdate ▸ componentWillUnmount
  • 27. KEYS
  • 28. KEYS var MyComponent = React.createClass({ getInitialState: function() { return { todos: ["Listen to Tupac", "Don't eat bread", "Put up Christmas decorations"] } }, render: function(){ var items = this.state.todos.map(function(todo){ return <li>{todo}</li> }) return ( <ul> {items} </ul> ) } }); React.render(<MyComponent/>, document.body);
  • 29. KEYS var MyComponent = React.createClass({ getInitialState: function() { return { todos: ["Listen to Tupac", "Don't eat bread", "Put up Christmas decorations"] } }, render: function(){ var items = this.state.todos.map(function(todo, index){ return <li key={index}>{todo}</li> }) return ( <ul> {items} </ul> ) } }); React.render(<MyComponent/>, document.body);
  • 30. REFS
  • 31. REFS var MyComponent = React.createClass({ handleSubmit: function(){ console.log(this.refs.nameInput.value) }, render: function(){ return ( <form onSubmit={this.handleSubmit}> <label htmlFor="nameInput">Enter your name:</label> <input type="text" name="nameInput" ref="nameInput"/> </form> ) } }); React.render(<MyComponent/>, document.body);
  • 33. EVENTS var MyComponent = React.createClass({ handleClick: function(){ alert('I Got Clicked!') }, render: function(){ return ( <a href="#" onClick={this.handleClick}/>Click Me</a> ) } }); React.render(<MyComponent/>, document.body);
  • 35. SERVER SIDE REACT // server.js var state = {todos: ["Get in a fistfight", "Buy chain wallet"]} var markup = React.renderToString(<MyComponent/>); res.render('index',{ markup: markup, state: state });
  • 36. SERVER SIDE REACT // index.handlebars <html> <head></head> <body> {{{ markup }}} <script id="app-state" type="application/json">{{{state}}}</script> </body> </html>
  • 37. SERVER SIDE REACT // client.js var state = JSON.parse(document.getElementById('app-state').innerHTML); React.render(<MyComponent state={state}/>, document.body);
  • 39. What Is Flux Architecture?
  • 40. FLUX ARCHITECTURE ▸ Global Pub/Sub System ▸ Actions send data and action type to Dispatcher ▸ Dispatcher sends actions to Stores & enforces dependencies ▸ Stores receive data, update their data and notify Views ▸ Views update & call actions
  • 42. THIS SUCKS var ChildComponent = React.createClass({ render: function(){ <a href="#" onClick={this.props.onClick}/> } }) var TopComponent = React.createClass({ handleClick: function(){ console.log('meh'); }, render: function(){ return ( <ChildComponent onClick={this.handleClick}/> ) } })
  • 46. Flux Architecture Using McFly http://kenwheeler.github.io/mcfly
  • 47. DISPATCHER // Dispatcher.js // Creates a new dispatcher and attaches it to mcFly.dispatcher var mcFly = new McFly(); module.exports = mcFly;
  • 48. ACTIONS // TodoActions.js var TodoActions = mcFly.createActions({ addTodo: function(todo){ return { actionType: 'ADD_TODO', todo: todo } } }) module.exports = TodoActions;
  • 49. STORES //TodoStore.js var _todos = []; function addTodo(todo){ _todos = _todos.push(todo); } var TodoStore = mcFly.createStore({ getTodos: function(){ return _todos; } }, function(payload){ if(payload.actionType === "ADD_TODO") { addTodo(payload.todo); TodoStore.emitChange(); } }) module.exports = TodoStore;
  • 50. VIEWS var TodoList = React.createClass({ mixins: ['TodoStore.mixin'], getInitialState: function(){ return { todos: TodoStore.getTodos() } }, addTodo: function(){ TodoActions.addTodo(this.refs.todoInput.value); }, render: function(){ var todos = this.state.todos.map(function(todo,index){ return <li key={index}>{todo}</li> }) return ( <div className="todos-app"> <ul className="todos"> {todos} </ul> <input type="text" ref="todoInput"/> <input type="submit" onClick={this.addTodo} value="Add Todo"/> </div> ) } })