javascript - ES6 React 组件中的 PureRenderMixin

标签 javascript reactjs ecmascript-6

有什么方法可以在 React ES6 类组件中包含 mixin 吗? (即 extends React.Component)?如果是这样,官方的做法是什么?

我想在我的一个具有不可变状态的组件中包含 PureRenderMixin 以加快差异化过程。

最佳答案

https://facebook.github.io/react/docs/shallow-compare.html

shallowCompare is a helper function to achieve the same functionality as PureRenderMixin while using ES6 classes with React.

import shallowCompare from 'react-addons-shallow-compare';

export default class SampleComponent extends React.Component {

  shouldComponentUpdate(nextProps, nextState) {
    // pure render
    return shallowCompare(this, nextProps, nextState);
  }

  render() {
    return <div className={this.props.className}>foo</div>;
  }
}

Source codePureRenderMixin是:

var ReactComponentWithPureRenderMixin = {
  shouldComponentUpdate: function(nextProps, nextState) {
    return shallowCompare(this, nextProps, nextState);
  },
};

所以,当你使用 PureRenderMixin ,你实际使用shallowCompare

更新15.3.0 :

Add React.PureComponent - a new base class to extend, replacing react-addons-pure-render-mixin now that mixins don't work with ES2015 classes.

关于javascript - ES6 React 组件中的 PureRenderMixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149016/

相关文章:

javascript - 两条线之间的填充区域 - Chart.js v2

javascript - 使用 setTimeout 在 React 中限制窗口滚动事件

javascript - 如何将 JSON 数据导入 Google 表格?

javascript - 两个具有不同 css 样式的打印按钮

javascript - 通过通配符匹配 URL

javascript - 数组中每个元素的新段落

reactjs - 重新渲染元素列表时 react NotFoundError,与 `google.maps.OverlayView` 集成

typescript - 将 TypeScript 编译为多个目标

javascript - 巴别塔 : The CLI has been moved into the package `babel-cli`

javascript - 如何在react js中解析json的循环