SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
React.js
or why DOM finally makes sense
JSX ⊙_⊙
/**	
  @jsx	
  React.DOM	
  */	
  
var	
  name	
  =	
  "Foo",	
  commentText	
  =	
  "Bar",	
  avatarUrl="Baz";	
  
	
  	
  
var	
  avatar	
  =	
  <img	
  src	
  =	
  {avatarUrl}/>;	
  
	
  	
  
var	
  comment	
  =	
  <div	
  className="comment">	
  
	
  	
  {avatar}	
  @{name}:	
  {commentText}	
  
</div>
JSX is nice!
⊙⌣⊙
!
var	
  name	
  =	
  "Foo",	
  commentText	
  =	
  "Bar",	
  avatarUrl="Baz";	
  
	
  	
  
var	
  avatar	
  =	
  React.DOM.img(	
  {src:	
  	
  avatarUrl});	
  
	
  	
  
var	
  comment	
  =	
  React.DOM.div(	
  {className:"comment"},	
  	
  
	
  	
  avatar,	
  "	
  @",name,":	
  ",	
  commentText	
  
)
/**	
  @jsx	
  React.DOM	
  */	
  
var	
  name	
  =	
  "Foo",	
  commentText	
  =	
  "Bar",	
  avatarUrl="Baz";	
  
	
  	
  
var	
  avatar	
  =	
  <img	
  src	
  =	
  {avatarUrl}/>;	
  
	
  	
  
var	
  comment	
  =	
  <div	
  className="comment">	
  
	
  	
  {avatar}	
  @{name}:	
  {commentText}	
  
</div>
JSX rocks!
⊙⌣⊙
• tags are functions
• you can use closures
• scope is straightforward
• normalized DOM
the rules!
• do not mix logic and presentation
• do not write inline javascript
FUCK!
the rules!
• do not mix logic and presentation
• do not write inline javascript
components

components
components
/**	
  @jsx	
  React.DOM	
  */	
  
var	
  User	
  =	
  React.createClass({	
  
	
  	
  render:	
  function()	
  {	
  
	
  	
  	
  	
  return	
  (<div>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <img	
  src={this.props.avatar}/>:	
  
	
  	
  	
  	
  	
  	
  	
  	
  @{this.props.username}	
  
	
  	
  	
  	
  	
  	
  </div>);	
  
	
  	
  }	
  
});	
  
!
React.renderComponent(	
  
	
  	
  <User	
  username="Eldar"	
  avatar="http://img.src/edjafarov"/>,	
  
	
  	
  document.getElementById('example')	
  
);
components

components
components
/**	
  @jsx	
  React.DOM	
  */	
  
var	
  Avatar	
  =	
  React.createClass({	
  
	
  	
  render:	
  function()	
  {	
  
	
  	
  	
  	
  return	
  <img	
  src={this.props.uri}/>;	
  
	
  	
  }	
  
});	
  
var	
  User	
  =	
  React.createClass({	
  
	
  	
  render:	
  function()	
  {	
  
	
  	
  	
  	
  return	
  (<div>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <Avatar	
  uri={this.props.avatar}/>:	
  
	
  	
  	
  	
  	
  	
  	
  	
  @{this.props.username}	
  
	
  	
  	
  	
  	
  	
  </div>);	
  
	
  	
  }	
  
});
components

components
components
Props & State
var	
  User	
  =	
  React.createClass({	
  
	
  	
  getInitialState:	
  function(){	
  
	
   	
  	
  return	
  {	
  
	
   	
   	
  	
  name:this.props.user.name,	
  
	
   	
   	
  	
  uri:	
  this.props.user.uri	
  
	
   	
  	
  }	
  
	
  	
  },	
  	
  	
   	
  
	
  	
  render:	
  function()	
  {	
  
	
  	
  	
  	
  return	
  (<div>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <Avatar	
  uri={this.props.avatar}/>:	
  
	
  	
  	
  	
  	
  	
  	
  	
  @{this.state.name}	
  
	
  	
  	
  	
  	
  	
  </div>);	
  
	
  	
  }	
  
});
Props & State
routing
doesn’t matter
http://visionmedia.github.io/page.js/
var	
  User	
  =	
  require('./User');	
  
	
  	
  
page('/user/:user',	
  user.load,	
  function(ctx){	
  
	
  	
  React.renderComponent(	
  
	
  	
  	
  	
  <User	
  user={ctx.user}/>,	
  
	
  	
  	
  	
  document.getElementById('root')	
  
	
  	
  );	
  
})
2 way binding/**	
  @jsx	
  React.DOM	
  */	
  
var	
  WithLink	
  =	
  React.createClass({	
  
	
  	
  mixins:	
  [React.addons.LinkedStateMixin],	
  
	
  	
  getInitialState:	
  function()	
  {	
  
	
  	
  	
  	
  return	
  {value:	
  'Hello!'};	
  
	
  	
  },	
  
	
  	
  render:	
  function()	
  {	
  
	
  	
  	
  	
  return	
  <input	
  type="text"	
  valueLink={this.linkState('value')}	
  />;	
  
	
  	
  }	
  
});
var	
  Hello	
  =	
  React.createClass({	
  
	
  	
  mixins:[ModelMixin,	
  BindMixin],	
  
	
  	
  getBackboneModels:	
  function(){	
  
	
  	
  	
  	
  return	
  [this.props.instance]	
  
	
  	
  },	
  
	
  	
  render:	
  function()	
  {	
  
	
  	
  	
  	
  var	
  model	
  =	
  this.props.instance;	
  
	
  	
  	
  	
  return	
  <div>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <div>Hello	
  {model.get(‘initial')}</div>	
  
	
  	
  	
  	
  	
  	
  	
  	
  <input	
  type="text"	
  valueLink={this.bindTo(model,	
  'initial')}/>	
  
	
  	
  	
  	
  	
  	
  </div>	
  
	
  	
  }	
  
});
models
or
emitters?
What if models and collections are not enough?
https://github.com/component/emitter
var	
  Emitter	
  =	
  require('emitter');	
  
var	
  UserModel	
  =	
  new	
  Emitter({}	
  
	
  	
  data:	
  {},	
  
	
  	
  update:	
  function(){	
  
	
  	
  	
  	
  //some	
  async	
  update	
  
	
  	
  	
  	
  this.data	
  =	
  newData;	
  
	
  	
  	
  	
  this.emit('change');	
  
	
  	
  	
  	
  //-­‐-­‐-­‐	
  
	
  	
  }	
  
);	
  
UserModel.on('change',	
  /*update	
  component*/);	
  	
  
//You	
  can	
  use	
  mixin	
  to	
  do	
  that	
  automatically
be Lazy with
factory
• get bunch of id’s
• get empty model’s from factory by these id’s
• pass them to component
• PROFIT!
Q&A
@edjafarov
eldar.djafarov.com
just visit
reactjs.com
✌

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

React & Redux
React & ReduxReact & Redux
React & Redux
 
Redux training
Redux trainingRedux training
Redux training
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
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
 
ReactJs presentation
ReactJs presentationReactJs presentation
ReactJs presentation
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
Let's Redux!
Let's Redux!Let's Redux!
Let's Redux!
 
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
 
React redux
React reduxReact redux
React redux
 
React Native: JS MVC Meetup #15
React Native: JS MVC Meetup #15React Native: JS MVC Meetup #15
React Native: JS MVC Meetup #15
 
React + Redux. Best practices
React + Redux.  Best practicesReact + Redux.  Best practices
React + Redux. Best practices
 
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
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
 
React on es6+
React on es6+React on es6+
React on es6+
 
ReactJS
ReactJSReactJS
ReactJS
 
Mobile Day - React Native
Mobile Day - React NativeMobile Day - React Native
Mobile Day - React Native
 
Introduction to React & Redux
Introduction to React & ReduxIntroduction to React & Redux
Introduction to React & Redux
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 

Similar a React.js or why DOM finally makes sense

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

Similar a React.js or why DOM finally makes sense (20)

Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentation
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Forever
 
JSLab. Алексей Волков. "React на практике"
JSLab. Алексей Волков. "React на практике"JSLab. Алексей Волков. "React на практике"
JSLab. Алексей Волков. "React на практике"
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
React lecture
React lectureReact lecture
React lecture
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)
 
Clean Javascript
Clean JavascriptClean Javascript
Clean Javascript
 
SilverStripe CMS JavaScript Refactoring
SilverStripe CMS JavaScript RefactoringSilverStripe CMS JavaScript Refactoring
SilverStripe CMS JavaScript Refactoring
 
React js
React jsReact js
React js
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
 
ReactJS
ReactJSReactJS
ReactJS
 
React 101
React 101React 101
React 101
 
Frontin like-a-backer
Frontin like-a-backerFrontin like-a-backer
Frontin like-a-backer
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React
React React
React
 
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
 
Beyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance JavascriptBeyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance Javascript
 
How to React Native
How to React NativeHow to React Native
How to React Native
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
 

Más de Eldar Djafarov

node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascript
Eldar Djafarov
 
Your project tested #nodejs
Your project tested #nodejsYour project tested #nodejs
Your project tested #nodejs
Eldar Djafarov
 

Más de Eldar Djafarov (6)

PromisePipe inception
PromisePipe inceptionPromisePipe inception
PromisePipe inception
 
The Grail: React based Isomorph apps framework
The Grail: React based Isomorph apps frameworkThe Grail: React based Isomorph apps framework
The Grail: React based Isomorph apps framework
 
Frontend - экосистема и будущее: iforum 2015
Frontend - экосистема и будущее: iforum 2015Frontend - экосистема и будущее: iforum 2015
Frontend - экосистема и будущее: iforum 2015
 
node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascript
 
Compy slides
Compy slidesCompy slides
Compy slides
 
Your project tested #nodejs
Your project tested #nodejsYour project tested #nodejs
Your project tested #nodejs
 

Último

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
 
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@
 
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)

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
 
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...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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...
 
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
 
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
 
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 ...
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
+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 - 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...
 
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
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
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
 
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
 
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
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

React.js or why DOM finally makes sense

  • 1. React.js or why DOM finally makes sense
  • 2. JSX ⊙_⊙ /**  @jsx  React.DOM  */   var  name  =  "Foo",  commentText  =  "Bar",  avatarUrl="Baz";       var  avatar  =  <img  src  =  {avatarUrl}/>;       var  comment  =  <div  className="comment">      {avatar}  @{name}:  {commentText}   </div>
  • 3. JSX is nice! ⊙⌣⊙ ! var  name  =  "Foo",  commentText  =  "Bar",  avatarUrl="Baz";       var  avatar  =  React.DOM.img(  {src:    avatarUrl});       var  comment  =  React.DOM.div(  {className:"comment"},        avatar,  "  @",name,":  ",  commentText   ) /**  @jsx  React.DOM  */   var  name  =  "Foo",  commentText  =  "Bar",  avatarUrl="Baz";       var  avatar  =  <img  src  =  {avatarUrl}/>;       var  comment  =  <div  className="comment">      {avatar}  @{name}:  {commentText}   </div>
  • 4. JSX rocks! ⊙⌣⊙ • tags are functions • you can use closures • scope is straightforward • normalized DOM
  • 5. the rules! • do not mix logic and presentation • do not write inline javascript
  • 6. FUCK! the rules! • do not mix logic and presentation • do not write inline javascript
  • 7. components
 components components /**  @jsx  React.DOM  */   var  User  =  React.createClass({      render:  function()  {          return  (<div>                  <img  src={this.props.avatar}/>:                  @{this.props.username}              </div>);      }   });   ! React.renderComponent(      <User  username="Eldar"  avatar="http://img.src/edjafarov"/>,      document.getElementById('example')   );
  • 8. components
 components components /**  @jsx  React.DOM  */   var  Avatar  =  React.createClass({      render:  function()  {          return  <img  src={this.props.uri}/>;      }   });   var  User  =  React.createClass({      render:  function()  {          return  (<div>                  <Avatar  uri={this.props.avatar}/>:                  @{this.props.username}              </div>);      }   });
  • 10. Props & State var  User  =  React.createClass({      getInitialState:  function(){        return  {          name:this.props.user.name,          uri:  this.props.user.uri        }      },            render:  function()  {          return  (<div>                  <Avatar  uri={this.props.avatar}/>:                  @{this.state.name}              </div>);      }   });
  • 12. routing doesn’t matter http://visionmedia.github.io/page.js/ var  User  =  require('./User');       page('/user/:user',  user.load,  function(ctx){      React.renderComponent(          <User  user={ctx.user}/>,          document.getElementById('root')      );   })
  • 13. 2 way binding/**  @jsx  React.DOM  */   var  WithLink  =  React.createClass({      mixins:  [React.addons.LinkedStateMixin],      getInitialState:  function()  {          return  {value:  'Hello!'};      },      render:  function()  {          return  <input  type="text"  valueLink={this.linkState('value')}  />;      }   }); var  Hello  =  React.createClass({      mixins:[ModelMixin,  BindMixin],      getBackboneModels:  function(){          return  [this.props.instance]      },      render:  function()  {          var  model  =  this.props.instance;          return  <div>                  <div>Hello  {model.get(‘initial')}</div>                  <input  type="text"  valueLink={this.bindTo(model,  'initial')}/>              </div>      }   });
  • 14. models or emitters? What if models and collections are not enough? https://github.com/component/emitter var  Emitter  =  require('emitter');   var  UserModel  =  new  Emitter({}      data:  {},      update:  function(){          //some  async  update          this.data  =  newData;          this.emit('change');          //-­‐-­‐-­‐      }   );   UserModel.on('change',  /*update  component*/);     //You  can  use  mixin  to  do  that  automatically
  • 15. be Lazy with factory • get bunch of id’s • get empty model’s from factory by these id’s • pass them to component • PROFIT!