(理论)React在进行组件更新时,如果发现这个组件是一个PureComponent,它会将组件现在的state和props和其下一个state和props进行浅比较,如果它们的值没有变化,就不会进行更新。
1.创建一个类组件
import React from 'react';
class Com extends React.Component {
constructor(props){
super(props);
this.state={
count: 0
}
}
render(){
return (
<div>
<span>{this.state.count}</span>
<button onClick={()=>{this.setState({count: 1})}}></button>
</div>
)
}
}
export default Com;
2.纯组件的使用
import React from 'react';
//-------------------------------------
class Com extends React.PureComponent {
//-------------------------------------
constructor(props){
super(props);
this.state={
count: 0
}
}
render(){
return (
<div>
<span>{this.state.count}</span>
<button onClick={()=>{this.setState({count: 1})}}></button>
</div>
)
}
}
export default Com;