Mar 2017 @vekzdran
...that will persuade your dev team to start using it!
15 min
15 min
included
+
var React = require("react");
var Link = require("react-router").Link;
var SearchBox = require("../components/SearchBox.js");
var Header = React.createClass({
render: function() {
return (
<div className="header">
<Link to="/">
<h1>Weather App</h1>
</Link>
<SearchBox className="searchBoxSmall" />
</div>
);
}
});
// CommonJS module approach
module.exports = Header;
... possibly even worse witchcraft than Angular 2!
render: function() {
// the <div>
return React.createElement("div", {className: "header"},
[
// the <Link>
React.createElement(Link, {to: "/"},
// the <h1>
React.createElement("h1", null, "Weather App")),
// the <SearchBox>
React.createElement(SearchBox, {className: "searchBoxSmall"}, null)
]
);
}
... syntactic sugar known as JSX
... that transforms to JavaScript!
import React from "React";
import { Link } from "react-router";
import SearchBox from "../components/SearchBox.js";
class Header extends React.Component {
render() {
return (
<div className="header">
<Link to="/">
<h1>Weather App</h1>
</Link>
<SearchBox className="searchBoxSmall" />
</div>
);
}
}
export default Header;
... basically the standard for writing React apps
JSX is like a healthy vegetable 🥕 that tastes like decadent chocolate 🍰 cake. You feel guilty, but it’s good for you.
Eric Elliot
return (
<BattleAlert gameId={this.props.gameId}>
<h1>Hello {username}, welcome to: {this.props.gameName}</h1>
<ConfirmBattle
isLoading={this.state.isLoading}
onInitiateBattle={this.handleInitiateBattle} />
</BattleAlert>
);
¯\_(ツ)_/¯
All it does is render stuff efficiently and delegate events.
It knows the data it needs to render.
It knows when to render it (when the data changes).
It knows how to render it efficiently.
E. Elliot
:)
...because they are:
UI components example again :)
-bind="text: starDate"
import PrettyPrint from "./PrettyPrint";
var Writer = React.createClass({
render: function () {
return (
<PrettyPrint text="Foo... Bar!" id={this.props.id} />
);
}
});
var PrettyPrint = React.createClass({
render: function() {
return (
<p id={this.props.id} className="aPrettyParagraph">
{this.props.text}
</p>
);
}
});
this.props is immutable
*also means functions, i.e. events
...JSX is just a bunch of .js nested React.createElement() calls
var PrintRandomNumber = React.createClass({
setRandomNumber: function() {
// changing state will fire render()
this.setState({ number: Math.random() });
}
render: function () {
return <PrettyPrint text="{this.state.number}" />;
}
});
// This a Stateless Functional Component
// They are pure functional transforms of their input, with zero boilerplate.
var ICanOnlyRenderWithProps = function(props) {
return (
<p id={props.id} className="statlessPrettyParagraph">
{this.props.text}
</p>
);
};
mutating state will fire render() and all child render() methods
var PrintRandomNumber = React.createClass({
setRandomNumber: function() {
// changing state will fire render()
this.setState({ number: Math.random() });
}
render: function () {
return <PrettyPrint text="{this.state.number}" />;
}
});
Mounting (component mounts DOM):
Updating (state mutates or new props come):
Unmounting:
getDefaultProps() -> getInitialState() -> componentWillMount()
render() -> children component(s) lifecycle
componentDidMount()
componentWillReceiveProps() -> shouldComponentUpdate() - >componentWillUpdate()
render() -> children component(s) lifecycle
componentDidUpdate()
componentWillUnmount() -> children component(s) lifecycle
Instances destroyed for GC
It's so complex and cool - South Park s14e10
that React + vDOM solve
React + Redux: 150kB
server rendering
pluggable on any page
Apples vs Orangutans? :)