[英]PureRenderMixin in an ES6 React component
有沒有辦法在React ES6類組件中包含mixin? (即extends React.Component
)? 如果是這樣,官方的做法是什么?
我想將PureRenderMixin
包含在我的一個具有不可變狀態的組件中,以加快差異化過程。
https://facebook.github.io/react/docs/shallow-compare.html
shallowCompare是一個輔助函數,可以在使用帶有React的ES6類時實現與PureRenderMixin相同的功能。
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>;
}
}
var ReactComponentWithPureRenderMixin = {
shouldComponentUpdate: function(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
},
};
因此,當您使用PureRenderMixin
,您實際上使用shallowCompare
更新15.3.0 :
添加
React.PureComponent
- 一個新的基類來擴展,替換react-addons-pure-render-mixin
,因為mixin不能與ES2015類一起使用。
根據文件,還沒有。
資料來源: http : //facebook.github.io/react/docs/reusable-components.html#no-mixins
如果我想在我的es6反應組件中使用mixins,我會使用react-mixin ,但我沒有嘗試使用PureRenderMixin
這個庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.