StaticContainer.react.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict-local
  9. */
  10. 'use strict';
  11. const React = require('react');
  12. /**
  13. * Renders static content efficiently by allowing React to short-circuit the
  14. * reconciliation process. This component should be used when you know that a
  15. * subtree of components will never need to be updated.
  16. *
  17. * const someValue = ...; // We know for certain this value will never change.
  18. * return (
  19. * <StaticContainer>
  20. * <MyComponent value={someValue} />
  21. * </StaticContainer>
  22. * );
  23. *
  24. * Typically, you will not need to use this component and should opt for normal
  25. * React reconciliation.
  26. */
  27. type Props = $ReadOnly<{|
  28. /**
  29. * Whether or not this component should update.
  30. */
  31. shouldUpdate?: ?boolean,
  32. /**
  33. * Content short-circuited by React reconciliation process.
  34. */
  35. children: React.Node,
  36. |}>;
  37. class StaticContainer extends React.Component<Props> {
  38. shouldComponentUpdate(nextProps: Props): boolean {
  39. return !!nextProps.shouldUpdate;
  40. }
  41. render(): React.Node {
  42. return this.props.children;
  43. }
  44. }
  45. module.exports = StaticContainer;