Christopher Bloom
Presentation: https://slides.com/illepic/pdxsass-intro-to-react-js
Demo: http://plnkr.co/gkHDuT
Demo (stream): http://plnkr.co/edit/?p=streamer&s=aNjEh1kRgwKGvYu6
/** @jsx React.DOM */
React.renderComponent(
<h1>React is awesome!</h1>,
document.getElementById('bloomPres')
);
ie Pattern Lab
If Javascript and XML had a baby but it didn't turn out as evil as that sounds."
If a complex UI widget changes something about itself (ie a dropdown opening, quantities of products updating) instead of re-rendering the entire DOM for that markup (like jQuery and Angular do), React runs a "diffing algorithm" on only what needs updating and changes only that. Requires type="text/jsx" on script tag.
<script type="text/jsx" src="script.js"></script>
var WidgetOne = React.createClass({
render: function(){
return (
<h1>Hello, world!</h1>
);
}
});
Now to stick it on a page:
React.renderComponent(
<WidgetOne/>,
document.getElementById('widgetOne')
);
var WidgetTwo = React.createClass({
render: function(){
return (
<h1>Hello, {this.props.name}!</h1>
);
}
});
React.renderComponent(
<WidgetTwo name="PDXSass" />,
document.getElementById('widgetTwo')
);
In an HTML-y sort of way, pass dynamic data to components.
var WidgetState = React.createClass({
getInitialState: function(){
return {
count: 5
}
},
render: function(){
return (
<h1>{this.state.count}</h1>
)
}
});
React.renderComponent(
<WidgetState name="PDXSass" />,
document.getElementById('widgetState')
);
State is a biggie. Using setState() we trigger data changes and re-rendering of our Component.
var WidgetEvents = React.createClass({
incrementCount: function(){
this.setState({
count: this.state.count + 1
});
},
getInitialState: function(){
return {
count: 0
}
},
render: function(){
return (
<div class="event-demo">
<h3>Count: {this.state.count}</h3>
<button type="button" onClick={this.incrementCount}>Increment</button>
</div>
);
}
});
React.renderComponent(
<WidgetEvents/>,
document.getElementById('widgetEvents')
);
Define a function on your component. Bind it with an onClick prop.
Simple: Data flows from the SINGLE PARENT down to its child elements using "state". The various "props" on the child elements pick up this state.
..
render: function(){
return (
<div className="bigParent">
<input type="text" placeholder="Search" onChange={this.changeStuff}/>
<PeopleList people={this.state.people}/>
</div>
);
}
...
var PeopleList= React.createClass({
render: function() {
return (
<ul className="people">
{
this.props.people.map(function(person) {
return <li className="person" key={person}>{person}</li>
})
}
</ul>
)
}
});
https://scotch.io/tutorials/getting-to-know-flux-the-react-js-architecture
View at: http://run.plnkr.co/plunks/gkHDuT/
Chrome plugin that shows data, props, state, components, and all sorts of other React goodies.