UNPKG

1.72 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule ReactComponentWithPureRenderMixin
10 */
11
12import isEqual from "./isEqual";
13function shallowCompare(instance, nextProps, nextState) {
14 return !isEqual(instance.props, nextProps, true) || !isEqual(instance.state, nextState, true);
15}
16
17/**
18 * If your React component's render function is "pure", e.g. it will render the
19 * same result given the same props and state, provide this mixin for a
20 * considerable performance boost.
21 *
22 * Most React components have pure render functions.
23 *
24 * Example:
25 *
26 * var ReactComponentWithPureRenderMixin =
27 * require('ReactComponentWithPureRenderMixin');
28 * React.createClass({
29 * mixins: [ReactComponentWithPureRenderMixin],
30 *
31 * render: function() {
32 * return <div className={this.props.className}>foo</div>;
33 * }
34 * });
35 *
36 * Note: This only checks shallow equality for props and state. If these contain
37 * complex data structures this mixin may have false-negatives for deeper
38 * differences. Only mixin to components which have simple props and state, or
39 * use `forceUpdate()` when you know deep data structures have changed.
40 *
41 * See https://facebook.github.io/react/docs/pure-render-mixin.html
42 */
43var ReactComponentWithPureRenderMixin = {
44 shouldComponentUpdate: function shouldComponentUpdate(nextProps, nextState) {
45 return shallowCompare(this, nextProps, nextState);
46 }
47};
48export default ReactComponentWithPureRenderMixin;
\No newline at end of file