React.js
Just shut up about it already, Pete
What is React?
A library for creating user interfaces using
reusable, composable components
Used as the “V” in
a client-side MVC framework
Not a templating engine!
Why use React?
Components, not templates
#1
Templates separate
technologies, not concerns
Display logic and markup are
tightly coupled and highly cohesive
Coupling
“The degree to which each program module relies on each of the other modules.”
Cohesion
“The degree to which elements of a module belong together”
App.SayHello = Ember.Component.extend({
actions: {
hello: function() {
alert('Hello ' + this.get('name'));
}
}
});
An example from Ember
Component class
<script type="text/x-handlebars" data-template-name="say-hello">
<button {{action 'hello'}}>
Say Hello to {{name}}
</button>
</script>
Handlebars template
var SayHello = React.createClass({
onClick: function(event) {
alert('Hello ' + this.props.name);
},
render: function() {
return (
<button onClick={this.onClick}>
Say Hello to {this.props.name}
</button>
);
}
});
Compared to React
Component Class
One way data flow
#2
Re-render the whole app on every update
The key design decision that makes React awesome
Managing UI State is hard
-
Mutable DOM
-
Mutable data
-
User input
-
Asynchronous API requests
Browser request
Fetch data from DB
Pass data to template
Template renders page
Like traditional server-side rendering
-
User input
-
Fetch data from API
-
Pass data to top component
-
React renders component tree
Traditional Rendering
React Rendering
No More
- Magical data binding
- Model dirty checking
- Explicit DOM manipulation
Benefits
- Easy to reason about
- Predictable
- Decreases coupling
React components are essentially idempotent functions
They describe your UI at any point in time.
Virtual DOM
#3
JavaScript is fast
DOM manipulation is slow
On every update…
-
React builds a virtual DOM tree
-
Diffs the tree with the previous tree
-
Computes the minimal set of DOM mutations
-
Batch executes from the mutation queue
Runs in Node.js
-
Isomorphic applications with shared components
-
Render initial state for search indexing and caching
-
React will start from initial state without re-rendering
React Examples
var HelloWorld = React.createClass({
render: function() {
return <p>Hello {this.props.name}</p>;
}
});
Hello React
React.render(
<HelloWorld name="John" />,
document.getElementById("app")
);
Rendering the component on the page:
What is JSX?
Quick detour
XML syntax extension for JavaScript
Concise & familiar syntax for defining tree structures
<p className="hello">
Hello {this.props.name}
</p>
How does React use JSX?
React.createElement("p", {className: "hello"},
"Hello ", this.props.name
)
Compiles to a React factory method:
<p className={this.props.className}>
Hello {this.props.name.toUpperCase()}!
</p>
React.createElement("p", {className: this.props.className},
"Hello ", this.props.name.toUpperCase(), "!"
)
JavaScript Expressions
<p>
Hello <a href="https://www.localmed.com">LocalMed</a>
</p>
Nested children
React.createElement("p", null,
"Hello ",
React.createElement("a", {href: "https://www.localmed.com"},
"LocalMed"
)
)
More React examples
Alright, back to
var ClickCounter = React.createClass({
getInitialState: function() {
return {clickCount: 0};
},
onClick: function() {
this.setState({clickCount: this.state.clickCount + 1});
},
render: function() {
return (
<p onClick={this.onClick}>
Clicks: {this.state.clickCount}
</p>
);
}
});
A stateful component
-
Owned by parent
-
Changed by parent
-
Preferred
When to use state?
-
Owned by component
-
Changed by component
-
Avoided
Props
State
Composition
var ClickApp = React.createClass({
getInitialState: function() {
return {clickCount: 0};
},
onClick: function() {
this.setState({clickCount: this.state.clickCount + 1});
},
render: function() {
return (
<div>
<h2>Click App</h2>
<a onClick={this.onClick}>Click me!</a>
<ClickCount count={this.state.clickCount} />
</div>
);
}
});
var ClickCount = React.createClass({
render: function() {
return <p>Clicks: {this.props.count}</p>;
}
});
var ClickCount = React.createClass({
propTypes: {
count: React.PropTypes.number.isRequired
},
render: function() {
return <p>Clicks: {this.props.count}</p>;
}
});
Property validation
Who uses React?
How is LocalMed using React?
Openings Calendar
Styleguides
…and what the hell
is Flux?
An architecture pattern for building client-side web applications
It complements React’s composable view components by utilizing a unidirectional data flow.
Flux Architecture
Flux implementations
Unfortunately, there’s no clear winner
-
Fluxxor
-
Reflux
-
Delorean
-
Fluxy
-
Marty
-
…
React.js
By Peter Browne
React.js
What is React and why should we use it?
- 1,645