react优化性能
来源:互联网 发布:淘宝申请代销 编辑:程序博客网 时间:2024/06/06 03:32
优化性能
Avoid Reconciliation
React
创建并保存一渲染UI的镜像,通常被称为virtual DOM
,当组件的props
或者state
发生变化时,React
会通过对比虚拟DOM和新返回的元素,如果二者不同,则更新DOM。
在某些情况下,可以通过shouldComponentUpdate
生命周期函数来加速这种对比的过程。此函数在重新渲染前触发,默认返回true
,React
执行更新:
shouldComponentUpdate(nextProps, nextState) { return true;}
如果你知道在哪种情况下,组件不需要更新,可以在此函数中返回false
来跳过整个渲染的过程,包括调用本组件和以后组件中的render()
shouldComponentUpdate In Action
SCU代表shouldComponentUpdate
返回值,绿色true
红色false
vDOMEq 代表DOM是否完全相同,绿色相同
红色不同
C2节点shouldComponentUpdate
返回false
,React
不需要更新C2,甚至不用再去和C4 C5对比了。
C1 C3 SCU返回true
,继续向下对比C6, SCU返回true
,vDOMEq检测不相同,更新C6
虽然C3 SCU返回true
,继续向下对比的时候C7跟以前渲染的DOM节点相同,所以不再更新
C8 对比DOM时相同,就不再更新了;而C2 C7时shouldComponentUpdate
返回了false
,所以甚至不用对比DOM,就可以直接跳过了,也不用调用render
示例 如果只有在props.color
和state.count
变化时才更新组件
class CounterButton extends React.Component { constructor(props) { super(props); this.state = {count: 1}; } shouldComponentUpdate(nextProps, nextState) { if (this.props.color !== nextProps.color) { return true; } if (this.state.count !== nextState.count) { return true; } return false; } render() { return ( <button color={this.props.color} onClick={() => this.setState(state => ({count: state.count + 1}))}> Count: {this.state.count} </button> ); }}
如果业务变的复杂了,这种逐一判断的方式就不太实用了,React
提供了一种隐式对比的helper
,继承自React.PureComponent
,以上代码就可以简化为:
class CounterButton extends React.PureComponent { constructor(props) { super(props); this.state = {count: 1}; } render() { return ( <button color={this.props.color} onClick={() => this.setState(state => ({count: state.count + 1}))}> Count: {this.state.count} </button> ); }}
值得注意的是,props
和state
如果以一种隐式判断不识别的方式写的时候,这种方法就不起作用了,比如有这样一个需求,父组件WordAdder
调用子组件ListOfWords
,每点击一次按钮,增加一个单词marklar
,按通常思维有可能会按以下代码写
class ListOfWords extends React.PureComponent { render() { return <div>{this.props.words.join(',')}</div>; }}class WordAdder extends React.Component { constructor(props) { super(props); this.state = { words: ['marklar'] }; this.handleClick = this.handleClick.bind(this); } handleClick() { // This section is bad style and causes a bug const words = this.state.words; words.push('marklar'); this.setState({words: words}); } render() { return ( <div> <button onClick={this.handleClick} /> <ListOfWords words={this.state.words} /> </div> ); }}
问题是PureComponent
只会把this.props.words
的新旧值做一个简单的对比。尽管数组words
在handleClick
方法中发生了改变,但是覆盖了上一个生命周期中的state
,所以在新旧对比的时候state
是相同的,不会触发重新渲染。 因此就有了下面要说的不修改数据
The Power Of Not Mutating Data
最简单的方法就是不修改数据,上面示例中的handleClick
可以重写为
handleClick() { this.setState(prevState => ({ words: prevState.words.concat(['marklar']) }));}//或者handleClick() { this.setState(prevState => ({ words: [...prevState.words, 'marklar'], }));};
- React-性能优化详解
- React Native性能优化
- React组件性能优化
- react性能优化
- react 性能优化工具
- React性能优化总结
- react优化性能
- React性能优化总结
- React Native性能优化
- React Native性能优化
- React 组件性能优化
- React Native 性能优化建议
- React性能检测和优化
- React性能优化 PureComponent 使用指南
- React 应用的性能优化思路
- react native白屏及性能优化
- react native 性能优化基础 - 官方文档
- react-native 性能优化,处理卡顿
- 【行业】以色列工人银行、微软就区块链技术开展合作
- 【研究】皇家墨尔本理工大学成立全球首个区块链学科研究中心
- 获取远程html插入到本地的网页里面(java+react实现)
- 【观点】比特币如何拯救飓风“哈维”的受灾者
- 今天被一个架构师面了
- react优化性能
- 初识H5
- SpringMVC中在使用@ResponseBody时如何将Date类型的日期返回指定格式的json数据
- lua TValue类型分析
- 【国际】美国总务署官员:区块链采购原型推进中
- 【国际】欧洲央行&日本央行:区块链尚不成熟,不能服务大型央行
- 【行业】安永联合微软和马士基在海上保险中使用区块链
- Oracle连接查询
- 使用Fiddler调试手机请求调用webapp