重写shouldComponentUpdate方法

import React, { Component } from 'react';
import { is } from 'immutable';
export default class PureComponent extends Component {
    shouldComponentUpdate(newProps, newState) {
        let oldState = this.state || {};
        let oldProps = this.props || {};
        newState = newState ? newState : {};
        if (Object.keys(oldState).length !== Object.keys(newState).length || Object.keys(oldProps).length !== Object.keys(newProps).length) {
            return true;
        }
        //属性的个数长度不一样更新,属性不相同更新,否则不更新
        for (let key in newState) {
            if (!is(newState[key], oldState[key])) {
                return true;
            }
        }

        for (const key in newProps) {
            if (!is(oldProps[key], newProps[key])) {
                return true;
            }
        }
        
        return false;
    }
}

Maldivee
145 声望4 粉丝

如果你能看到我的梦想,请给我前行的力量,幸运是忠诚的祈祷,是光穿梭的暗道