Dec 2017, ATD13, @vekzdran
...that will persuade you(r dev team) to start using it!
👏👏🏻👏🏼👏🏽👏🏾👏🏿
80%
how it looks
what it is? + philosophy
the data(flow)
the lifecycle
the virtual DOM
recap with pros/cons
20%
demo & questions
included
trolling, sarcasm and 😀
one intentional bug 🐛
n <= 10000
var React = require("react");
var createReactClass = require("create-react-class");
var Link = require("react-router").Link;
var SearchBox = require("../components/SearchBox.js");
var Header = createReactClass({
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, 4 and 5
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, { Component } from "React";
import { Link } from "react-router";
import SearchBox from "../components/SearchBox.js";
class Header extends 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 (any JavaScript app) React apps
ES6 transpiles into ES5/4/3 standards with e.g. Babel Transpiler
"JSX is like a healthy vegetable 🥕 that tastes like decadent chocolate 🍰 cake. You feel guilty, but it’s good for you."
Eric Elliot
NodeJS, npm, yarn, Webpack,
rollup, Headless Chrome, LightHouse...
...HTML in .JS(X)?! 😨
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 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.createReactClass({
render: function () {
return (
<PrettyPrint text="Foo... Bar!" id={this.props.id} />
);
}
});
this.props is immutable
*also means functions, i.e. events
var PrettyPrint = createReactClass({
render: function() {
return (
<p id={this.props.id} className="aPrettyParagraph">
{this.props.text}
</p>
);
}
});
...JSX is just a bunch of .js nested React.createElement() calls
var PrintRandomNumber = createReactClass({
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'
// Pure functional transforms of 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 = createReactClass({
setRandomNumber: function() {
// changing state will fire render()
this.setState({ number: Math.random() });
}
render: function () {
return <PrettyPrint text="{this.state.number}" />;
}
});
ahm... also getInitialState() is missing :)
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
Mounting:
Updating (on data change):
Unmounting:
getInitialState() -> onLoad
render() -> onRefreshUI + run nested control events
componentDidMount() -> onAfterUIRefreshedFirstTime
shouldComponentUpdate() - > onBeforeRefreshUI
render() -> onRefreshUI + run nested control events
componentDidUpdate() -> onAfterRefreshUI
componentWillUnmount() -> onUnload + run nested control events
It's so complex and cool - South Park s14e10
that React + vDOM solve
React + Redux: < 42kB
server rendering
pluggable on any page
Apples VS Orangutans?
ReactDOM.render(<AtdDemo />, rootEl);