Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shouldComponentUpdate() #93

Open
gracekrcx opened this issue Apr 5, 2020 · 0 comments
Open

shouldComponentUpdate() #93

gracekrcx opened this issue Apr 5, 2020 · 0 comments

Comments

@gracekrcx
Copy link
Owner

gracekrcx commented Apr 5, 2020

Immutable 详解及 React 中实践
Immutable.js 以及在 react+redux 项目中的实践
這篇文章提到 immitable js 与 React 搭配使用,Pure Render
在 shouldComponentUpdate() 做了些 render 優化
先記下來

import { is } from 'immutable';

shouldComponentUpdate: (nextProps = {}, nextState = {}) => {
  const thisProps = this.props || {}, thisState = this.state || {};

  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 (thisState[key] !== nextState[key] && !is(thisState[key], nextState[key])) {
      return true;
    }
  }
  return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant