gpt4 book ai didi

javascript - ES6 React 组件中的 PureRenderMixin

转载 作者:可可西里 更新时间:2023-11-01 02:51:55 25 4
gpt4 key购买 nike

有什么方法可以在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com