SlideShare una empresa de Scribd logo
1 de 53
Descargar para leer sin conexión
Ben Newman (@benjamn)
Paul O’Shannessy (@zpao)
React
reactjs.org
Components
<div>, <span>
<ActionButton>, <Counter>
Anatomy of a Component
<ActionButton text="Book flight" onAction={someFunc} />
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
!
!
!
!
!
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
!
!
!
!
!
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
return (	
<button class="ActionButton">	
<span>button text</span>	
</button>	
);	
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
return (	
<button class="ActionButton">	
<span>{this.props.text}</span>	
</button>	
);	
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
return (	
<button class="ActionButton" onClick={this.props.onAction}>	
<span>{this.props.text}</span>	
</button>	
);	
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
return (	
<button class="ActionButton" onClick={this.props.onAction}>	
<span>{this.props.text}</span>	
</button>	
);	
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
return (	
<button class="ActionButton" onClick={this.props.onAction}>	
<span>{this.props.text.toUpperCase()}</span>	
</button>	
);	
}	
});
<ActionButton text="Book flight" onAction={someFunc} />
var ActionButton = React.createClass({	
render: function() {	
return (	
<button class="ActionButton" onClick={this.props.onAction}>	
<span>{this.props.text}</span>	
</button>	
);	
}	
});
<Counter initialCount={4} />
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
What makes React different?
1. Components, not templates
2. Re-render on update
3. Virtual DOM (and events)
1. Components, not templates
Separation of
concerns:
!
Reduce coupling, increase cohesion.
Coupling is:
“The degree to which each program
module relies on each of the other
modules.”
http://en.wikipedia.org/wiki/Coupling_(computer_science)
Cohesion is:
“The degree to which elements of a
module belong together.”
http://en.wikipedia.org/wiki/Cohesion_(computer_science)
“View model” tightly
couples template to
display logic.
[{“price”: “7.99”, “product”: “Back
scratcher”, “tableRowColor”: “rgba(0, 0, 0,
0.5)”}]
Templates separate
technologies, not
concerns
React components are loosely
coupled and highly cohesive
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
2. Re-render on every change
<Counter initialCount={4} />
var Counter = React.createClass({	
getInitialState: function() {	
return {count: this.props.initialCount};	
},	
addToCount: function(delta) {	
this.setState({count: this.state.count + delta})	
},	
render: function() {	
return (	
<div>	
<h3>Count: {this.state.count}</h3>	
<ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} />	
<ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} />	
</div>	
);	
}	
});
- no DOM mutations
- no bindings between data and DOM
- in general, way less shit to think about
Best analogy: Website from 1994
Data changing over time is the
root of all evil.
Re-rendering on
every change makes
things simple.
Every place data is displayed is guaranteed
to be up-to-date.
No magical data binding.
Re-rendering on
every change makes
things simple.
No model dirty checking.
Re-rendering on
every change makes
things simple.
No more explicit DOM operations –
everything is declarative.
Re-rendering on
every change makes
things simple.
3. Virtual DOM
Won’t rerendering be as slow as
molasses?!
React has a virtual DOM (and
events system).
Optimized for performance and
memory footprint
On every update…
• React builds a new virtual DOM
subtree
• …diffs it with the old one
• …computes the minimal set of DOM
mutations and puts them in a queue
• …and batch executes all updates
It’s fast!
Because the DOM is slow!
It’s fast!
Computes minimal DOM operations
It’s fast!
Batched reads and writes for optimal DOM
performance
It’s fast!
Usually faster than manual DOM
operations
It’s fast!
Automatic top-level event delegation (with
cross-browser HTML5 events)
It’s fast!
Can do all this at 60fps, even in a (non-JIT)
UIWebView on the iPhone.
Why Should YOU Use React?
• Can be used for parts of your application
• Plays well with other libraries and technologies

(meteor, rails, node)
• Components allow you to split work easily
Learn more and get involved
• http://reactjs.org
• #reactjs on Freenode IRC
• reactjs on Google Groups
• www.facebook.com/careers
More Links
• react-meteor: https://github.com/benjamn/react-meteor
• <ActionButton> demo: http://jsfiddle.net/zpao/EFhy4/
• <Clicker> demo: http://jsfiddle.net/zpao/fk5Pc/
ReactJS

Más contenido relacionado

La actualidad más candente

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
Eldar Djafarov
 

La actualidad más candente (20)

React.js & Om: A hands-on walkthrough of better ways to build web UIs
React.js & Om: A hands-on walkthrough of better ways to build web UIsReact.js & Om: A hands-on walkthrough of better ways to build web UIs
React.js & Om: A hands-on walkthrough of better ways to build web UIs
 
React
React React
React
 
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 state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
React, Redux, ES2015 by Max Petruck
React, Redux, ES2015   by Max PetruckReact, Redux, ES2015   by Max Petruck
React, Redux, ES2015 by Max Petruck
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
 
Promises, Promises
Promises, PromisesPromises, Promises
Promises, Promises
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
React Lifecycle and Reconciliation
React Lifecycle and ReconciliationReact Lifecycle and Reconciliation
React Lifecycle and Reconciliation
 
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
 
React & redux
React & reduxReact & redux
React & redux
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
Avoiding callback hell with promises
Avoiding callback hell with promisesAvoiding callback hell with promises
Avoiding callback hell with promises
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
React Fundamentals - Jakarta JS, Apr 2016
React Fundamentals - Jakarta JS, Apr 2016React Fundamentals - Jakarta JS, Apr 2016
React Fundamentals - Jakarta JS, Apr 2016
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Intro to ReactJS
Intro to ReactJSIntro to ReactJS
Intro to ReactJS
 
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
 
Promise pattern
Promise patternPromise pattern
Promise pattern
 

Destacado

Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...
Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...
Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...
Kai Wähner
 
Bill gates powerpoint
Bill gates powerpointBill gates powerpoint
Bill gates powerpoint
masonwilson1
 
Bill gates leadership & personality traits
Bill gates leadership & personality traitsBill gates leadership & personality traits
Bill gates leadership & personality traits
Akhil Pillai
 

Destacado (20)

ReactJs
ReactJsReactJs
ReactJs
 
ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는
 
Datomic – A Modern Database - StampedeCon 2014
Datomic – A Modern Database - StampedeCon 2014Datomic – A Modern Database - StampedeCon 2014
Datomic – A Modern Database - StampedeCon 2014
 
Probability with Cards
Probability with CardsProbability with Cards
Probability with Cards
 
Unity 5: First-Person Tutorial
Unity 5: First-Person TutorialUnity 5: First-Person Tutorial
Unity 5: First-Person Tutorial
 
Fast, In-Memory SQL on Apache Cassandra with Apache Ignite (Rachel Pedreschi,...
Fast, In-Memory SQL on Apache Cassandra with Apache Ignite (Rachel Pedreschi,...Fast, In-Memory SQL on Apache Cassandra with Apache Ignite (Rachel Pedreschi,...
Fast, In-Memory SQL on Apache Cassandra with Apache Ignite (Rachel Pedreschi,...
 
Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...
Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...
Real World Use Cases and Success Stories for In-Memory Data Grids (TIBCO Acti...
 
Elon Musk and his innovations
Elon Musk and his innovationsElon Musk and his innovations
Elon Musk and his innovations
 
19 Mini Productivity Hacks For A Simple (But An Awesome) Day
19 Mini Productivity Hacks For A Simple (But An Awesome) Day19 Mini Productivity Hacks For A Simple (But An Awesome) Day
19 Mini Productivity Hacks For A Simple (But An Awesome) Day
 
Inside BCG's Smart Simplicity Approach
Inside BCG's Smart Simplicity ApproachInside BCG's Smart Simplicity Approach
Inside BCG's Smart Simplicity Approach
 
Entrepreneur Elon musk
Entrepreneur   Elon muskEntrepreneur   Elon musk
Entrepreneur Elon musk
 
Bill gates powerpoint
Bill gates powerpointBill gates powerpoint
Bill gates powerpoint
 
Leadership by Elon Musk with Tesla and SpaceX
Leadership by Elon Musk with Tesla and SpaceXLeadership by Elon Musk with Tesla and SpaceX
Leadership by Elon Musk with Tesla and SpaceX
 
Bill gates leadership & personality traits
Bill gates leadership & personality traitsBill gates leadership & personality traits
Bill gates leadership & personality traits
 
Elon Musk- Visionary, Leader, Entrepreneur
Elon Musk- Visionary, Leader, EntrepreneurElon Musk- Visionary, Leader, Entrepreneur
Elon Musk- Visionary, Leader, Entrepreneur
 
Smart Cities – how to master the world's biggest growth challenge
Smart Cities – how to master the world's biggest growth challengeSmart Cities – how to master the world's biggest growth challenge
Smart Cities – how to master the world's biggest growth challenge
 
Datomic
DatomicDatomic
Datomic
 
Datomic
DatomicDatomic
Datomic
 
Datomic
DatomicDatomic
Datomic
 
Waldorf Education
Waldorf EducationWaldorf Education
Waldorf Education
 

Similar a ReactJS

How to create a magento controller in magento extension
How to create a magento controller in magento extensionHow to create a magento controller in magento extension
How to create a magento controller in magento extension
Hendy Irawan
 

Similar a ReactJS (20)

React 101
React 101React 101
React 101
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
Full Stack Toronto - the 3R Stack
Full Stack Toronto - the 3R StackFull Stack Toronto - the 3R Stack
Full Stack Toronto - the 3R Stack
 
Road to react hooks
Road to react hooksRoad to react hooks
Road to react hooks
 
Backbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCBackbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVC
 
Learning React: Facebook's Javascript Library For Building User Interfaces
Learning React: Facebook's Javascript Library For Building User InterfacesLearning React: Facebook's Javascript Library For Building User Interfaces
Learning React: Facebook's Javascript Library For Building User Interfaces
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
Introduction to React and MobX
Introduction to React and MobXIntroduction to React and MobX
Introduction to React and MobX
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
How do we use hooks
How do we use hooksHow do we use hooks
How do we use hooks
 
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
 
Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016
 
How to create a magento controller in magento extension
How to create a magento controller in magento extensionHow to create a magento controller in magento extension
How to create a magento controller in magento extension
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
How to perform debounce in react
How to perform debounce in reactHow to perform debounce in react
How to perform debounce in react
 
Client Web
Client WebClient Web
Client Web
 
Recompacting your react application
Recompacting your react applicationRecompacting your react application
Recompacting your react application
 
Redux js
Redux jsRedux js
Redux js
 
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
 

Más de Kamlesh Singh (8)

Angular js book
Angular js bookAngular js book
Angular js book
 
Smart machines
Smart machinesSmart machines
Smart machines
 
Spring roo-docs
Spring roo-docsSpring roo-docs
Spring roo-docs
 
Database Oracle Basic
Database Oracle BasicDatabase Oracle Basic
Database Oracle Basic
 
Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Java Basic day-1
Java Basic day-1Java Basic day-1
Java Basic day-1
 
Jvm
JvmJvm
Jvm
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

ReactJS

  • 1.
  • 2. Ben Newman (@benjamn) Paul O’Shannessy (@zpao) React reactjs.org
  • 4. Anatomy of a Component
  • 5. <ActionButton text="Book flight" onAction={someFunc} />
  • 6. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { ! ! ! ! ! } });
  • 7. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { ! ! ! ! ! } });
  • 8. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton"> <span>button text</span> </button> ); } });
  • 9. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton"> <span>{this.props.text}</span> </button> ); } });
  • 10. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text}</span> </button> ); } });
  • 11. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text}</span> </button> ); } });
  • 12. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text.toUpperCase()}</span> </button> ); } });
  • 13. <ActionButton text="Book flight" onAction={someFunc} /> var ActionButton = React.createClass({ render: function() { return ( <button class="ActionButton" onClick={this.props.onAction}> <span>{this.props.text}</span> </button> ); } });
  • 15. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } });
  • 16. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } });
  • 17. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } });
  • 18. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } });
  • 19. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } });
  • 20. What makes React different?
  • 21. 1. Components, not templates 2. Re-render on update 3. Virtual DOM (and events)
  • 22.
  • 23.
  • 24. 1. Components, not templates
  • 26. Coupling is: “The degree to which each program module relies on each of the other modules.” http://en.wikipedia.org/wiki/Coupling_(computer_science)
  • 27. Cohesion is: “The degree to which elements of a module belong together.” http://en.wikipedia.org/wiki/Cohesion_(computer_science)
  • 28. “View model” tightly couples template to display logic. [{“price”: “7.99”, “product”: “Back scratcher”, “tableRowColor”: “rgba(0, 0, 0, 0.5)”}]
  • 30. React components are loosely coupled and highly cohesive
  • 31. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } });
  • 32. 2. Re-render on every change
  • 33. <Counter initialCount={4} /> var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, addToCount: function(delta) { this.setState({count: this.state.count + delta}) }, render: function() { return ( <div> <h3>Count: {this.state.count}</h3> <ActionButton text="+1" onAction={this.addToCount.bind(this, 1)} /> <ActionButton text="-1" onAction={this.addToCount.bind(this, -1)} /> </div> ); } }); - no DOM mutations - no bindings between data and DOM - in general, way less shit to think about
  • 35. Data changing over time is the root of all evil.
  • 36. Re-rendering on every change makes things simple. Every place data is displayed is guaranteed to be up-to-date.
  • 37. No magical data binding. Re-rendering on every change makes things simple.
  • 38. No model dirty checking. Re-rendering on every change makes things simple.
  • 39. No more explicit DOM operations – everything is declarative. Re-rendering on every change makes things simple.
  • 41. Won’t rerendering be as slow as molasses?!
  • 42. React has a virtual DOM (and events system). Optimized for performance and memory footprint
  • 43. On every update… • React builds a new virtual DOM subtree • …diffs it with the old one • …computes the minimal set of DOM mutations and puts them in a queue • …and batch executes all updates
  • 44. It’s fast! Because the DOM is slow!
  • 46. It’s fast! Batched reads and writes for optimal DOM performance
  • 47. It’s fast! Usually faster than manual DOM operations
  • 48. It’s fast! Automatic top-level event delegation (with cross-browser HTML5 events)
  • 49. It’s fast! Can do all this at 60fps, even in a (non-JIT) UIWebView on the iPhone.
  • 50. Why Should YOU Use React? • Can be used for parts of your application • Plays well with other libraries and technologies
 (meteor, rails, node) • Components allow you to split work easily
  • 51. Learn more and get involved • http://reactjs.org • #reactjs on Freenode IRC • reactjs on Google Groups • www.facebook.com/careers
  • 52. More Links • react-meteor: https://github.com/benjamn/react-meteor • <ActionButton> demo: http://jsfiddle.net/zpao/EFhy4/ • <Clicker> demo: http://jsfiddle.net/zpao/fk5Pc/