constFoo=({foo})=>{console.log('Foo is rendering!');return(<div>Foo{foo}</div>);}constBar=({bar})=>{console.log('Bar is rendering!');return(<div>Bar{bar}</div>);}constFooBarGroup=({foo,bar})=>{console.log('FooBar is rendering!');return(<div><Foofoo={foo}/><Barbar={bar}/></div>)}classAppextendsReact.Component{constructor(props){super(props)this.state={foo:0,bar:0};this.handleFooClick=this.handleFooClick.bind(this);this.handleBarClick=this.handleBarClick.bind(this);}handleFooClick(e){e.preventDefault();constnewFoo=this.state.foo+1;this.setState({foo:newFoo});}handleBarClick(e){e.preventDefault();constnewBar=this.state.bar+1;this.setState({bar:newBar});}render(){const{foo,bar}=this.state;return(<divclassName="App"><buttononClick={this.handleFooClick}>Foo</button><buttononClick={this.handleBarClick}>Bar</button><FooBarGroupfoo={foo}bar={bar}/></div>);}}
classFooextendsReact.Component{shouldComponentUpdate(nextProps){returnthis.props.foo!==nextProps.foo;}render(){console.log('Foo is rendering!');return(<div>{this.props.foo}</div>)}}constBar=({bar})=>{console.log('Bar is rendering!');return(<div>{bar}</div>);}constFooBarGroup=({foo,bar})=>{console.log('FooBarGroup is rendering!');return(<div><Foofoo={foo}/><Barbar={bar}/></div>)}
classFooextendsPureComponent{constructor(props){super(props);this.state={foo:['test']}}handleClick=(e)=>{e.preventDefault();constfoo=this.state.foo;foo.push('test');//push是一个mutable的操作,foo的引用并没有改变
this.setState({foo});}render(){console.log('Foo is rendering!');return(<div><buttononClick={this.handleClick}>Foobalabala</button>{this.state.foo.length}</div>)}}
constFoo=({foo})=>{console.log('Foo is rendering!');return(<div>{foo}</div>);}constBar=({bar})=>{console.log('Bar is rendering!');return(<div>{bar}</div>);}constFooBarGroup=({foo,bar})=>{console.log('FooBar is rendering!');return(<div><Foofoo={foo}/><Barbar={bar}/></div>)}classPureRendererextendsReact.PureComponent{render(){console.log('PureRenderer is rendering!!');const{text}=this.props;return(<div>{text}</div>)}}classAppextendsReact.Component{constructor(props){super(props)this.state={foo:0,bar:0}}handleFooClick=(e)=>{e.preventDefault();constnewFoo=this.state.foo+1;this.setState({foo:newFoo});}handleBarClick=(e)=>{e.preventDefault();constnewBar=this.state.bar+1;this.setState({bar:newBar});}render(){const{foo,bar}=this.state;return(<divclassName="App"><buttononClick={this.handleFooClick}>Foo</button><buttononClick={this.handleBarClick}>Bar</button><FooBarGroupfoo={foo}bar={bar}/><PureRenderertext="blablabla"onClick={()=>console.log('blablabla')}/></div>);}}