look nice
do nothing on their own
=> add a controller
presentation & functionality
receive input
props
render()
state
props
render()
state
what does this look like?
in-memory representation
before actual page render
compute diffs
<div>
<p>Hi!</p>
<p>10:45 AM</p>
<div><div>
<p>Hi!</p>
<p>10:45 AM</p>
<div><div>
<p>Hi!</p>
<p>10:46 AM</p>
<div><div>
<p>Hi!</p>
<p>10:46 AM</p>
<div>do fast things often
do slow things
only when needed
as much work as possible
class Parent {
constructor() {
this.obj = {val: 1};
this.onClick = this.onClick.bind(this);
}
onClick() {
this.obj.val += 1;
}
render() {
return (
<div>
<Child input={this.obj}/>
<button onClick={this.onClick} />
</div>
);
}
}class Child {
shouldComponentUpdate(nextProps, nextState) {
return this.props.obj.val !==
nextProps.obj.val;
}
render() {
return <p> {this.props.obj.val} </p>;
}
}class Parent {
constructor() {
this.obj = {val: 1};
this.onClick = this.onClick.bind(this);
}
onClick() {
this.obj.val += 1;
}
render() {
return (
<div>
<Child input={this.obj}/>
<button onClick={this.onClick} />
</div>
);
}
}class Child {
shouldComponentUpdate(nextProps, nextState) {
return this.props.obj.val !==
nextProps.obj.val;
}
render() {
return <p> {this.props.obj.val} </p>;
}
}u are mutating obj!
X
class Parent {
constructor() {
this.obj = Map({val: 1});
this.onClick = this.onClick.bind(this);
}
onClick() {
this.obj = this.obj.update(
'val',
val => val + 1
);
}
render() {
return (
<div>
<Child input={this.obj}/>
<button onClick={this.onClick} />
</div>
);
}
}class Child {
constructor() {
this.shouldComponentUpdate =
PureRenderMixin.shouldComponentUpdate
.bind(this);
}
render() {
return <p> {this.props.obj.val} </p>;
}
}