首页 > 其他 > 详细

React-组件性能优化一

时间:2021-05-14 21:08:04      阅读:24      评论:0      收藏:0      [点我收藏+]

案例:

  父组件引入子组件,在父组件更新内容时,子组件也会重新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;

 

React-组件性能优化一

原文:https://www.cnblogs.com/jwyblogs/p/14769611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!