Mar 2018, DEVUGZG, @vekzdran
...that will persuade you(r dev team) to start using it!
>98%
how it looks
what it is? + philosophy
the data(flow)
the lifecycle
the virtual DOM
recap with pros/cons
???%
demo & questions
included
jokes, sarcasm and 😀
one intentional bug 🐛
n <= 10000
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> recursively calls React.createElement in itself
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, Babel transpiler, TypeScript, Webpack, rollup, Headless Chrome, LightHouse...
Chrome updates every 3 minutes... why is Addy Osmani doing this?
...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>
);
- 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 transformation 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:
It's so complex and cool - South Park s14e10
SRC: https://www.huffingtonpost.com/2014/04/04/american-forests-big-tree_n_5094798.html?slideshow=true#gallery/314956/26
that React + vDOM solve
React + Redux: < 42kB
TypeScript/Flow
pluggable on any page
E2E test frameworks
Apples VS Orangutans?
ReactDOM.render(<DevUgZgDemo />, rootEl);