1 | // Type definitions for react-loadable 5.5
|
2 | // Project: https://github.com/thejameskyle/react-loadable#readme
|
3 | // Definitions by: Jessica Franco <https://github.com/Jessidhia>
|
4 | // Oden S. <https://github.com/odensc>
|
5 | // Ian Ker-Seymer <https://github.com/ianks>
|
6 | // Tomek Łaziuk <https://github.com/tlaziuk>
|
7 | // Ian Mobley <https://github.com/iMobs>
|
8 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
9 | // TypeScript Version: 3.7
|
10 |
|
11 | /// <reference types="react" />
|
12 |
|
13 | declare namespace LoadableExport {
|
14 | interface LoadingComponentProps {
|
15 | isLoading: boolean;
|
16 | pastDelay: boolean;
|
17 | timedOut: boolean;
|
18 | error: any;
|
19 | retry: () => void;
|
20 | }
|
21 |
|
22 | type Options<Props, Exports extends object> = OptionsWithoutRender<Props> | OptionsWithRender<Props, Exports>;
|
23 |
|
24 | interface CommonOptions {
|
25 | /**
|
26 | * React component displayed after delay until loader() succeeds. Also responsible for displaying errors.
|
27 | *
|
28 | * If you don't want to render anything you can pass a function that returns null
|
29 | * (this is considered a valid React component).
|
30 | */
|
31 | loading: React.ComponentType<LoadingComponentProps>;
|
32 | /**
|
33 | * Defaults to 200, in milliseconds.
|
34 | *
|
35 | * Only show the loading component if the loader() has taken this long to succeed or error.
|
36 | */
|
37 | delay?: number | false | null | undefined;
|
38 | /**
|
39 | * Disabled by default.
|
40 | *
|
41 | * After the specified time in milliseconds passes, the component's `timedOut` prop will be set to true.
|
42 | */
|
43 | timeout?: number | false | null | undefined;
|
44 |
|
45 | /**
|
46 | * Optional array of module paths that `Loadable.Capture`'s `report` function will be applied on during
|
47 | * server-side rendering. This helps the server know which modules were imported/used during SSR.
|
48 | * ```ts
|
49 | * Loadable({
|
50 | * loader: () => import('./my-component'),
|
51 | * modules: ['./my-component'],
|
52 | * });
|
53 | * ```
|
54 | */
|
55 | modules?: string[] | undefined;
|
56 |
|
57 | /**
|
58 | * An optional function which returns an array of Webpack module ids which you can get
|
59 | * with require.resolveWeak. This is used by the client (inside `Loadable.preloadReady`) to
|
60 | * guarantee each webpack module is preloaded before the first client render.
|
61 | * ```ts
|
62 | * Loadable({
|
63 | * loader: () => import('./Foo'),
|
64 | * webpack: () => [require.resolveWeak('./Foo')],
|
65 | * });
|
66 | * ```
|
67 | */
|
68 | webpack?: (() => Array<string | number>) | undefined;
|
69 | }
|
70 |
|
71 | interface OptionsWithoutRender<Props> extends CommonOptions {
|
72 | /**
|
73 | * Function returning a promise which returns a React component displayed on success.
|
74 | *
|
75 | * Resulting React component receives all the props passed to the generated component.
|
76 | */
|
77 | loader(): Promise<React.ComponentType<Props> | { default: React.ComponentType<Props> }>;
|
78 | }
|
79 |
|
80 | interface OptionsWithRender<Props, Exports extends object> extends CommonOptions {
|
81 | /**
|
82 | * Function returning a promise which returns an object to be passed to `render` on success.
|
83 | */
|
84 | loader(): Promise<Exports>;
|
85 | /**
|
86 | * If you want to customize what gets rendered from your loader you can also pass `render`.
|
87 | *
|
88 | * Note: If you want to load multiple resources at once, you can also use `Loadable.Map`.
|
89 | *
|
90 | * ```ts
|
91 | * Loadable({
|
92 | * // ...
|
93 | * render(loaded, props) {
|
94 | * const Component = loaded.default;
|
95 | * return <Component {...props} />
|
96 | * }
|
97 | * });
|
98 | * ```
|
99 | */
|
100 | render(loaded: Exports, props: Props): React.ReactNode;
|
101 |
|
102 | // NOTE: render is not optional if the loader return type is not compatible with the type
|
103 | // expected in `OptionsWithoutRender`. If you do not want to provide a render function, ensure that your
|
104 | // function is returning a promise for a React.ComponentType or is the result of import()ing a module
|
105 | // that has a component as its `default` export.
|
106 | }
|
107 |
|
108 | interface OptionsWithMap<Props, Exports extends { [key: string]: any }> extends CommonOptions {
|
109 | /**
|
110 | * An object containing functions which return promises, which resolve to an object to be passed to `render` on success.
|
111 | */
|
112 | loader: {
|
113 | [P in keyof Exports]: () => Promise<Exports[P]>
|
114 | };
|
115 | /**
|
116 | * If you want to customize what gets rendered from your loader you can also pass `render`.
|
117 | *
|
118 | * Note: If you want to load multiple resources at once, you can also use `Loadable.Map`.
|
119 | *
|
120 | * ```ts
|
121 | * Loadable({
|
122 | * // ...
|
123 | * render(loaded, props) {
|
124 | * const Component = loaded.default;
|
125 | * return <Component {...props} />
|
126 | * }
|
127 | * });
|
128 | * ```
|
129 | */
|
130 | render(loaded: Exports, props: Props): React.ReactNode;
|
131 | }
|
132 |
|
133 | interface LoadableComponent {
|
134 | /**
|
135 | * The generated component has a static method preload() for calling the loader function ahead of time.
|
136 | * This is useful for scenarios where you think the user might do something next and want to load the
|
137 | * next component eagerly.
|
138 | *
|
139 | * Note: preload() intentionally does not return a promise. You should not be depending on the timing of
|
140 | * preload(). It's meant as a performance optimization, not for creating UI logic.
|
141 | */
|
142 | preload(): void;
|
143 | }
|
144 |
|
145 | interface LoadableCaptureProps {
|
146 | /**
|
147 | * Function called for every moduleName that is rendered via React Loadable.
|
148 | */
|
149 | report: (moduleName: string) => void;
|
150 | }
|
151 |
|
152 | interface Loadable {
|
153 | <Props, Exports extends object>(options: Options<Props, Exports>): React.ComponentType<Props> & LoadableComponent;
|
154 | Map<Props, Exports extends { [key: string]: any }>(options: OptionsWithMap<Props, Exports>): React.ComponentType<Props> & LoadableComponent;
|
155 |
|
156 | /**
|
157 | * This will call all of the LoadableComponent.preload methods recursively until they are all
|
158 | * resolved. Allowing you to preload all of your dynamic modules in environments like the server.
|
159 | * ```ts
|
160 | * Loadable.preloadAll().then(() => {
|
161 | * app.listen(3000, () => {
|
162 | * console.log('Running on http://localhost:3000/');
|
163 | * });
|
164 | * });
|
165 | * ```
|
166 | */
|
167 | preloadAll(): Promise<void>;
|
168 |
|
169 | /**
|
170 | * Check for modules that are already loaded in the browser and call the matching
|
171 | * `LoadableComponent.preload` methods.
|
172 | * ```ts
|
173 | * window.main = () => {
|
174 | * Loadable.preloadReady().then(() => {
|
175 | * ReactDOM.hydrate(
|
176 | * <App/>,
|
177 | * document.getElementById('app'),
|
178 | * );
|
179 | * });
|
180 | * };
|
181 | * ```
|
182 | */
|
183 | preloadReady(): Promise<void>;
|
184 |
|
185 | Capture: React.ComponentType<LoadableCaptureProps>;
|
186 | }
|
187 | }
|
188 |
|
189 | declare const LoadableExport: LoadableExport.Loadable;
|
190 |
|
191 | /* eslint-disable-next-line @definitelytyped/no-declare-current-package */
|
192 | declare module "react-loadable" {
|
193 | export = LoadableExport;
|
194 | }
|