注意
這玩意也已經被React.PureComponent
的功能取代了,這裏依舊是提一下(主要是React.v15
的版本中的react-with-addons.js
,這些玩意還存在,哎,害人吶)
引入
import shallowCompare from 'react-addons-shallow-compare' // ES6
var shallowCompare = require('react-addons-shallow-compare') // ES5 with npm
var shallowCompare = React.addons.shallowCompare; // ES5 with react-with-addons.js
1.概要
和PureRenderMixin
一樣,如果你的React
組件的渲染函數是一個純函數也就是說對於相同的值返回一樣的結果同時不影響原元素,在某些場景下,你可以利用這個插件來極大地提升性能,和前面那一篇博客一樣的話,嘿嘿。
class SampleComponent extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
render() {
return <div className={this.props.className}>foo</div>;
}
}
說來也只是實現了React.PuerComponent
的功能,或許在這個組件構建之外還有點用,然而React
就是用來構造組件的。
下一篇將講
React
的LinkedStateMixin
(雖然這玩意已經被棄用,有趣)