1.把this指向的绑定放在constructor
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
这样可以保证this指向的绑定在整个函数只执行一次,后续再补充
2.虚拟dom利用Key值比对,同层比对来提升比对的速度。
3.借助sholdComponentUpdate(){}让子组件除了在值改变的情况下,只更新一次
eg:
如下面的子组件模块,其唯一的数据就是从父组件接受的content
render() {
const { content } = this.props;
return (
<div onClick={this.handleClick}>
{content}
</div>
)
}
可以按下面这样写shouldComponentUpdate()
来提升性能
//子组件被渲染一次之后不会再执行
shouldComponentUpdate(prevProps, nextProps) {
if (nextProps.content !== this.props.content) {
return true;
}
return false;
}