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