console
class ClickCounter extends React.Component{
constructor(props){
super(props);
console.log('ClickCounter initiated (constructor done)');
}
componentWillMount(){
console.log('ClickCounter will be mounted (componentWillMount)');
}
componentDidMount(){
console.log('ClickCounter mounted (componentDidMount)');
}
componentWillReceiveProps(newP){
console.log('ClickCounter will receive new Property: '+ `{count: ${newP.count}}`);
}
shouldComponentUpdate(newProps, newState){
if(newProps.count >5){
console.log('ClickCounter should not update, and it will be unmounted immediately');
ReactDOM.unmountComponentAtNode(document.querySelector('#container10'));
return false;
}
console.log('ClickCounter should be updated');
return true;
}
componentWillUpdate(newProps,newState){
console.log('ClickCounter will be updated ');
}
componentDidUpdate(currentP, currentS){
console.log('ClickCounter has been updated');
}
render(){
var counterStyle={
fontSize:80,
fontFamily: 'sans-serif',
fontWeight:'bold',
padding:'15px 0px'
}
console.log('ClickCounter is being rendered (about to return in render)');
return <div style= {counterStyle}>{this.props.count}</div>
}
componentWillUnmount(){
console.log('Component will be unmounted');
}
}
class ClickCounterParent extends React.Component{
constructor(props){
super(props);
this.state={
count:0
}
}
increase(){
this.setState({count: this.state.count+1});
}
render(){
var parentStyle={
width: 260,
height: 200,
textAlign: 'center',
backgroundColor: '#ffeb3a',
borderRadius: 20,
padding: 10
};
var buttonStyle={
width: 30,
height: 30,
fontWeight: 'bold',
lineHeight: 1,
fontSize: 20
}
return <div style={parentStyle}>
<ClickCounter count={this.state.count}></ClickCounter>
<button style={buttonStyle} onClick={this.increase.bind(this)}>+</button>
</div>
}
}
ReactDOM.render(
<ClickCounterParent />,
document.querySelector('#container10')
);
<div id="container10">
</div>
body{
background-color: #FCFCFC;
}
.container p {
font-size: 25px;
}