0
点赞
收藏
分享

微信扫一扫

react性能优化

未定义变量 2022-04-03 阅读 50
react.js

使用 shouldComponentUpdate(){

return true/false

}

默认为true刷新 false为不刷新 必须返回true/false

纯组件 PureComponent

自动对比 上一次的state/props和更改后的state/props的值 若一样则不刷新

当为对象时 应首先创建一个新对象再调用setState修改值

class Hello extends React.Component{
render(){
return (<div><Child></Child></div>)}
}

class Child extends React.PureComponent{
state={
count:1
}
add=()=>{
this.setState({
count:Math.floor(Math.random()*3)
})
}
render(){
return <button onClick={this.add}>+1</button>
}
}

举报

相关推荐

0 条评论