【React】类组件渲染性能优化

背景:

  • 实现一个功能:父组件变化了,子组件是静态的,不需要变化,所以render函数不想再次执行

PureComponnet

PureComponnet:如果接收到的新属性或者是更改后的状态和原属性、原状态相同的话,就不会去重新render了

在里面也可以使用shouldComponentUpdate,而且。是否重新渲染以shouldComponentUpdate的返回值为最终的决定因素

  • 原始代码:
import React ,{Component} from 'react'
// PureComponent  和shouldComponentUpdate 功能一样
// PureComponent  和shouldComponentUpdate 都有,优先执行和shouldComponentUpdate
import ReactDOM  from 'react-dom'
// 子组件
class FooPure extends Component {
    // 初始化数据
    constructor(props){
        super(props);
        this.state = {};
    }
    render(){
        console.log("FooPure子组件渲染了");
        return(<div><h2>我是FooPure子组件</h2><br/></div>)
    }
}
class App extends Component {
    constructor(props) {
        console.log('constructor 运行了');
        super(props);
        this.state = {
            type: true
        };
    }
    changeType = () => {
        this.setState({ type:!this.state.type })
        console.log('按钮被点击了');
    }
    // 渲染数据
    render(){
        const { type} = this.state;
        return(
            <div>
                <button onClick={this.changeType}>点击按钮</button>{type}
                <FooPure />
            </div>
        )
    }
}
ReactDOM.render(
 <App />,
  document.getElementById('root')
)

在这里插入图片描述


  • 使用PureComponnet实现需求:
import React ,{Component,PureComponent} from 'react'
// PureComponent  和shouldComponentUpdate 功能一样
// PureComponent  和shouldComponentUpdate 都有,优先执行和shouldComponentUpdate
import ReactDOM  from 'react-dom'
// 子组件
class FooPure extends PureComponent {
    // 初始化数据
    constructor(props){
        super(props);
        this.state = {};
    }
    // shouldComponentUpdate(){
    //     return true;
    // }
    render(){
        console.log("FooPure子组件渲染了");
        return(<div><h2>我是FooPure子组件</h2><br/></div>)
    }
}
class App extends Component {
    constructor(props) {
        console.log('constructor 运行了');
        super(props);
        this.state = {
            type: true
        };
    }
    changeType = () => {
        this.setState({ type:!this.state.type })
        console.log('按钮被点击了');
    }
    // 渲染数据
    render(){
        const { type} = this.state;
        return(
            <div>
                <button onClick={this.changeType}>点击按钮</button>{type}
                <FooPure />
            </div>
        )
    }
}
ReactDOM.render(
 <App />,
  document.getElementById('root')
)

在这里插入图片描述

shouldComponentUpdate

  • 作用:阻止了未发生变化的组件的再次渲染,增加了性能
  • 局限性:对于传递的参数是对象或数组时,判断不出来对象或数组的内部元素是否发生了变化
  • nextProps :将要从父组件传过来的props的值
  • nextState :将要改变的state的值
  • 必须写返回值,需要更新返回true,不需要更新返回false

  • 原始代码:
import React ,{Component} from 'react'
import ReactDOM  from 'react-dom'
// 子组件
class Foo extends Component {
    // 初始化数据
    constructor(props){
        super(props);
        this.state = {};
    }
    render(){
        console.log("Foo子组件渲染了");
        return(<div><h2>我是Foo子组件</h2><br/></div>)
    }
}
class App extends Component {
    constructor(props) {
        console.log('constructor 运行了');
        super(props);
        this.state = {
            type: true
        };
    }
    changeType = () => {
        this.setState({ type:!this.state.type })
        console.log('按钮被点击了');
    }
    // 渲染数据
    render(){
        const { type} = this.state;
        return(
            <div>
                <button onClick={this.changeType}>点击按钮</button>{type}
                <Foo />
            </div>
        )
    }
}
ReactDOM.render(
 <App />,
  document.getElementById('root')
)

在这里插入图片描述


  • 使用shouldComponentUpdate实现需求:
import React ,{Component} from 'react'
import ReactDOM  from 'react-dom'
// 子组件
class Foo extends Component {
    // 初始化数据
    constructor(props){
        super(props);
        this.state = {};
    }
    shouldComponentUpdate(nextProps, nextState) {
        if (nextProps.type === this.props.type) {
            return false;
        } else {
            return true;
        }

    }
    render(){
        console.log("Foo子组件渲染了");
        return(<div><h2>我是Foo子组件</h2><br/></div>)
    }
}
class App extends Component {
    constructor(props) {
        console.log('constructor 运行了');
        super(props);
        this.state = {
            type: true
        };
    }
    changeType = () => {
        this.setState({ type:!this.state.type })
        console.log('按钮被点击了');
    }
    // 渲染数据
    render(){
        const { type} = this.state;
        return(
            <div>
                <button onClick={this.changeType}>点击按钮</button>{type}
                <Foo />
            </div>
        )
    }
}
ReactDOM.render(
 <App />,
  document.getElementById('root')
)

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一颗不甘坠落的流星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值