UNPKG

3.52 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12var __assign = (this && this.__assign) || Object.assign || function(t) {
13 for (var s, i = 1, n = arguments.length; i < n; i++) {
14 s = arguments[i];
15 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
16 t[p] = s[p];
17 }
18 return t;
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21var React = require("react");
22/**
23 * Returns a new React component, ready to be instantiated.
24 * Note the closure here protecting Component, and providing a unique
25 * instance of Component to the static implementation of `load`.
26 */
27function asyncComponent(_a) {
28 var loader = _a.loader, Placeholder = _a.Placeholder;
29 var Component = null; // keep Component in a closure to avoid doing this stuff more than once
30 return /** @class */ (function (_super) {
31 __extends(AsyncRouteComponent, _super);
32 function AsyncRouteComponent(props) {
33 var _this = _super.call(this, props) || this;
34 _this.updateState = _this.updateState.bind(_this);
35 _this.state = {
36 Component: Component,
37 };
38 return _this;
39 }
40 /**
41 * Static so that you can call load against an uninstantiated version of
42 * this component. This should only be called one time outside of the
43 * normal render path.
44 */
45 AsyncRouteComponent.load = function () {
46 return loader().then(function (ResolvedComponent) {
47 Component = ResolvedComponent.default || ResolvedComponent;
48 });
49 };
50 AsyncRouteComponent.getInitialProps = function (ctx) {
51 // Need to call the wrapped components getInitialProps if it exists
52 if (Component !== null) {
53 return Component.getInitialProps
54 ? Component.getInitialProps(ctx)
55 : Promise.resolve(null);
56 }
57 };
58 AsyncRouteComponent.prototype.componentWillMount = function () {
59 AsyncRouteComponent.load().then(this.updateState);
60 };
61 AsyncRouteComponent.prototype.updateState = function () {
62 // Only update state if we don't already have a reference to the
63 // component, this prevent unnecessary renders.
64 if (this.state.Component !== Component) {
65 this.setState({
66 Component: Component,
67 });
68 }
69 };
70 AsyncRouteComponent.prototype.render = function () {
71 var ComponentFromState = this.state.Component;
72 if (ComponentFromState) {
73 return React.createElement(ComponentFromState, __assign({}, this.props));
74 }
75 if (Placeholder) {
76 return React.createElement(Placeholder, __assign({}, this.props));
77 }
78 return null;
79 };
80 return AsyncRouteComponent;
81 }(React.Component));
82}
83exports.asyncComponent = asyncComponent;
84//# sourceMappingURL=asyncComponent.js.map
\No newline at end of file