react性能优化的周期函数是什么

这篇文章主要讲解了“react性能优化的周期函数是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“react性能优化的周期函数是什么”吧!

创新互联公司自2013年创立以来,先为龙马潭等服务建站,龙马潭等地企业,进行企业商务咨询服务。为龙马潭企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

react性能优化是shouldComponentUpdate周期函数;该函数可判断是否需要调用render方法重新描绘dom,能够优化dom diff算法,语法为“shouldComponentUpdate(Props,state)”。

本教程操作环境:Windows10系统、react16.4.0版、Dell G3电脑。

react性能优化是哪个周期函数

shouldComponentUpdate 这个方法用来判断是否需要调用render方法重新描绘dom。因为dom的描绘非常消耗性能,如果我们能在shouldComponentUpdate方法中能够写出更优化的dom diff算法,可以极大的提高性能

shouldComponentUpdate() 方法格式如下:

shouldComponentUpdate(nextProps, nextState)

shouldComponentUpdate() 方法会返回一个布尔值,指定 React 是否应该继续渲染,默认值是 true, 即 state 每次发生变化组件都会重新渲染。

shouldComponentUpdate() 的返回值用于判断 React 组件的输出是否受当前 state 或 props 更改的影响,当 props 或 state 发生变化时,shouldComponentUpdate() 会在渲染执行之前被调用。

以下实例演示了 shouldComponentUpdate() 方法返回 false 时执行的操作(点击按钮无法修改):





React 实例





class Header extends React.Component {   constructor(props) {     super(props);     this.state = {favoritesite: "runoob"};   }   shouldComponentUpdate() {     return false;   }   changeSite = () => {     this.setState({favoritesite: "google"});   }   render() {     return (       
      

我喜欢的网站是 {this.state.favoritesite}

      修改       
    );   } } ReactDOM.render(, document.getElementById('root'));

输出结果:

react性能优化的周期函数是什么

以下实例演示了 shouldComponentUpdate() 方法返回 true 时执行的操作(点击按钮可以修改):





React 实例





class Header extends React.Component {   constructor(props) {     super(props);     this.state = {favoritesite: "runoob"};   }   shouldComponentUpdate() {     return true;   }   changeSite = () => {     this.setState({favoritesite: "google"});   }   render() {     return (       
      

我喜欢的网站是 {this.state.favoritesite}

      修改       
    );   } } ReactDOM.render(, document.getElementById('root'));

输出结果:

react性能优化的周期函数是什么

点击按钮后:

react性能优化的周期函数是什么

感谢各位的阅读,以上就是“react性能优化的周期函数是什么”的内容了,经过本文的学习后,相信大家对react性能优化的周期函数是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


当前标题:react性能优化的周期函数是什么
本文来源:http://myzitong.com/article/jiscpp.html

其他资讯