ContainerRender.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  2. import _createClass from "@babel/runtime/helpers/esm/createClass";
  3. import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
  4. import _inherits from "@babel/runtime/helpers/esm/inherits";
  5. import _createSuper from "@babel/runtime/helpers/esm/createSuper";
  6. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  7. import React from 'react';
  8. import ReactDOM from 'react-dom';
  9. /**
  10. * @deprecated Since we do not need support React15 any more.
  11. * Will remove in next major version.
  12. */
  13. var ContainerRender = /*#__PURE__*/function (_React$Component) {
  14. _inherits(ContainerRender, _React$Component);
  15. var _super = _createSuper(ContainerRender);
  16. function ContainerRender() {
  17. var _this;
  18. _classCallCheck(this, ContainerRender);
  19. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  20. args[_key] = arguments[_key];
  21. }
  22. _this = _super.call.apply(_super, [this].concat(args));
  23. _defineProperty(_assertThisInitialized(_this), "removeContainer", function () {
  24. if (_this.container) {
  25. ReactDOM.unmountComponentAtNode(_this.container);
  26. _this.container.parentNode.removeChild(_this.container);
  27. _this.container = null;
  28. }
  29. });
  30. _defineProperty(_assertThisInitialized(_this), "renderComponent", function (props, ready) {
  31. var _this$props = _this.props,
  32. visible = _this$props.visible,
  33. getComponent = _this$props.getComponent,
  34. forceRender = _this$props.forceRender,
  35. getContainer = _this$props.getContainer,
  36. parent = _this$props.parent;
  37. if (visible || parent._component || forceRender) {
  38. if (!_this.container) {
  39. _this.container = getContainer();
  40. }
  41. ReactDOM.unstable_renderSubtreeIntoContainer(parent, getComponent(props), _this.container, function callback() {
  42. if (ready) {
  43. ready.call(this);
  44. }
  45. });
  46. }
  47. });
  48. return _this;
  49. }
  50. _createClass(ContainerRender, [{
  51. key: "componentDidMount",
  52. value: function componentDidMount() {
  53. if (this.props.autoMount) {
  54. this.renderComponent();
  55. }
  56. }
  57. }, {
  58. key: "componentDidUpdate",
  59. value: function componentDidUpdate() {
  60. if (this.props.autoMount) {
  61. this.renderComponent();
  62. }
  63. }
  64. }, {
  65. key: "componentWillUnmount",
  66. value: function componentWillUnmount() {
  67. if (this.props.autoDestroy) {
  68. this.removeContainer();
  69. }
  70. }
  71. }, {
  72. key: "render",
  73. value: function render() {
  74. return this.props.children({
  75. renderComponent: this.renderComponent,
  76. removeContainer: this.removeContainer
  77. });
  78. }
  79. }]);
  80. return ContainerRender;
  81. }(React.Component);
  82. _defineProperty(ContainerRender, "defaultProps", {
  83. autoMount: true,
  84. autoDestroy: true,
  85. forceRender: false
  86. });
  87. export { ContainerRender as default };