React-组件性能优化一

案例:

  父组件引入子组件,在父组件更新内容时,子组件也会重新render

import React, { Component } from 'react';

class ChildComponent extends Component {
    
    render() {
        console.log('child render')
        return <div>child</div>;
    }
}

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 1
        };
    }
    render() {
        return (
            <div>
                <ChildComponent />
                <p>count:{this.state.count}</p>
                <button onClick={this.clickFn}>点击</button>
            </div>
        );
    }

    clickFn = () => {
        this.setState({
            count: ++this.state.count
        });
    };
}

  实现:当父组件中改变数据时,不需要子组件进行重新渲染

  解决:

    1.使用生命周期,shouldComponentUpdate

import React, { Component } from 'react';

class ChildComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            type: 'add'
        };
    }
    shouldComponentUpdate(nextProps, nextState) {
        return nextProps.type !== this.state.type;
    }
    render() {
        console.log('child render');
        return <div>child</div>;
    }
}

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 1,
            type: 'add'
        };
    }
    render() {
        return (
            <div>
                <ChildComponent type={this.state.type} />
                <p>count:{this.state.count}</p>
                <button onClick={this.clickFn}>点击</button>
            </div>
        );
    }

    clickFn = () => {
        this.setState({
            count: ++this.state.count
        });
    };
}

   2.使用PureComponent

import React, { Component, PureComponent } from 'react';

class ChildComponent extends PureComponent {
    render() {
        console.log('child render');
        return <div>child</div>;
    }
}

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 1,
         
        };
    }
    render() {
        return (
            <div>
                <ChildComponent />
                <p>count:{this.state.count}</p>
                <button onClick={this.clickFn}>点击</button>
            </div>
        );
    }

    clickFn = () => {
        this.setState({
            count: ++this.state.count
        });
    };
}

  3.使用memo

import React, { Component, PureComponent, memo } from 'react';

class ChildComponent extends Component {
    render() {
        console.log('child render');
        return <div>child</div>;
    }
}

const Child = memo(ChildComponent);

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 1
        };
    }
    render() {
        return (
            <div>
                <Child />
                <p>count:{this.state.count}</p>
                <button onClick={this.clickFn}>点击</button>
            </div>
        );
    }

    clickFn = () => {
        this.setState({
            count: ++this.state.count
        });
    };
}

  函数式组件

import React, { memo, useMemo, useState } from 'react';

// 子组件
const Sub = memo(() => {
    console.log('child components render');
    return <div>child</div>;
});

function App() {
    const [age, setAge] = useState(20);
    console.log('parent components render');
    return (
        <div>
            <Sub />
            <button onClick={() => setAge(age + 3)}>改变年龄</button>
        </div>
    );
}

export default App;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值