PureRenderMixin.js 2.0 KB

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