React
by stephenkingsley
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
React
class JOKER extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
count: props.count,
}
}
componentWillReceiveProps(nextProps) {
console.log('I am JOKER\'s componentWillReceiveProps--->');
this.setState({ count: nextProps.count });
}
render() {
console.log('I am JOKER\'s render--->');
const { count } = this.state;
return (
<div>
<p style={{ color: 'red' }}>JOKER: You clicked {count} times</p>
</div>
);
}
}
class DC extends React.PureComponent {
constructor() {
super();
this.state = {
count: 0,
};
}
render() {
const { count } = this.state;
return (
<div>
<button onClick={() => {
this.setState({ count: count + 1, });
console.log('\n')
}}>
Click me
</button>
<p>You clicked {count} times</p>
<JOKER count={count} />
</div>
);
}
}
ReactDOM.render(<DC />, document.querySelector("#app"))