immutable在React中使用

import React from 'react';
import {is} from 'immutable';
 
class BaseComponent extends React.Component {
    constructor(props, context, updater) {
        super(props, context, updater);
    }
 
    shouldComponentUpdate(nextProps, nextState) {
        const thisProps = this.props || {};
        const thisState = this.state || {};
        nextState = nextState || {};
        nextProps = nextProps || {};
 
        if (Object.keys(thisProps).length !== Object.keys(nextProps).length ||
            Object.keys(thisState).length !== Object.keys(nextState).length) {
            return true;
        }
 
        for (const key in nextProps) {
            if (!is(thisProps[key], nextProps[key])) {
                return true;
            }
        }
 
        for (const key in nextState) {
            if (!is(thisState[key], nextState[key])) {
                return true;
            }
        }
        return false;
    }
}
 
export default BaseComponent;
posted @ 2018-12-18 17:51  FE-神鸟  阅读(261)  评论(0编辑  收藏  举报