React从入门到精通系列之(16)性能优化
十六、性能优化在React内部,React使用了几种比较聪明的技术来实现 对于许多应用来说,使用React将很快速的渲染出用户界面,从而无需进行大量工作来专门做优化性能的工作。 大概有以下有几种方法来加快你的React应用程序。 使用生产环境的配置进行构建如果你在React应用中进行基准测试或这遇到了性能问题,请首先确保你是使用的压缩后线上版本js文件来进行的测试:
new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production') } }),new webpack.optimize.UglifyJSPlugin(); 在构建应用程序时开发构建工具可以打印一些有帮助的额外警告。 避免重复处理DOMReact会创建并维护所渲染的UI内部表示信息。其中包括从组件返回的React元素。 此表示信息使React避免创建DOM节点和访问那些没有必要的节点,因为这样做可能会比JavaScript对象上的一些操作更慢。 有时它被称为 当组件的 在某些情况下,您的组件可以通过重写生命周期函数 shouldComponentUpdate(nextProps,nextState) { return true; } 如果你知道在某些情况下你的组件不需要更新,你可以从 shouldComponentUpdate的应用这里是一个组件的子树。 对于其中每一个子树来说,
因为 对于 最后一个有趣的例子是 注意,React只需要做 例子如果你的组件的唯一的改变方式就是改变 import React from 'react'; import ReactDOM from 'react-dom'; class CounterButton extends React.Component { constructor(props) { super(props); this.state = {count: 1}; this.click = this.click.bind(this); } click() { this.setState(prevState => ({ count: prevState.count + 1 })); } shouldComponentUpdate(nextProps,nextState) { if (this.props.color !== nextProps.color) { return true; } return this.state.count !== nextState.count; } render() { return ( <button color={this.props.color} onClick={this.click}> Count:{this.state.count} </button> ); } } ReactDOM.render( <CounterButton color="blue"/>,document.getElementById('root') ); 在这段代码中, 比较常见的模式是使用React提供的一个帮助对象来使用这个逻辑,可以直接继承 import React from 'react'; import ReactDOM from 'react-dom'; class CounterButton extends React.PureComponent { constructor(props) { super(props); this.state = {count: 1}; this.click = this.click.bind(this); } click() { this.setState(prevState => ({ count: prevState.count + 1 })); } render() { return ( <button color={this.props.color} onClick={this.click}> Count: {this.state.count} </button> ); } } ReactDOM.render( <CounterButton color="blue"/>,document.getElementById('root') ); 大多数时候,你可以使用
// PureComponent在内部会帮我们对props和state进行简单对比(浅比较) // 值类型比较值,引用类型比较引用,但是不会比较引用类型的内部数据是否改变。 // 所以就会出现一个bug,不管你怎么点button,div是不会增加的。 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: ['zhangyatao']}; this.click = this.click.bind(this); } click() { // 这么写是不对的,因为state的更新是异步的,所以可能会导致一些不必要的bug const words = this.state.word; words.push('zhangyatao'); this.setState({words: words}); } render() { return ( <div> <button onClick={this.click} /> <ListOfWords words={this.state.words} /> </div> ); } } 问题是 超能力之『不会突然变化的数据』避免此问题的最简单的方法就是避免将那些 click() { this.setState(prevState => ({ count: prevState.words.concat(['zhangyatao']) })); } ES6支持数组的 click() { this.setState(prevState => ({ words: [...prevState.words,'zhangyatao'] })); } 您还可以把那部分 function updateColorMap(colormap) { colormap.right = 'blue'; } 要将上面的代码写成不会濡染改变的对象,我们可以使用 function updateColorMap(colormap) { return Object.assign(colormap,{right: 'blue'}); }
有一个JavaScript提议来添加对象 function updateColorMap(colormap) { return {...colormap,right: 'blue'}; } 如果使用 使用不突变的数据结构
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |