async-import-react-component
Version:
Support react components import asynchronously, with support for code splitting
45 lines (44 loc) • 2.1 kB
TypeScript
import React, { ReactNode, ComponentType } from 'react';
declare type ModuleNamespace<T> = {
default: ComponentType<T>;
};
declare type PromiseFun<T> = () => Promise<ModuleNamespace<T>>;
interface Options<T> {
resolve: PromiseFun<T>;
loading?: ReactNode;
error?: ReactNode;
delay?: number;
}
interface State<T> {
readonly ResultComponent: ComponentType<T> | null;
readonly status: 'loading' | 'error' | 'normal';
}
declare function getAsyncComponent<T>(options: Options<T> | PromiseFun<T>): {
new (props: T): {
componentDidMount(): Promise<void>;
componentWillUnmount(): void;
render(): {};
context: any;
setState<K extends "ResultComponent" | "status">(state: State<T> | ((prevState: Readonly<State<T>>, props: Readonly<T>) => State<T> | Pick<State<T>, K>) | Pick<State<T>, K>, callback?: () => void): void;
forceUpdate(callback?: () => void): void;
readonly props: Readonly<T> & Readonly<{
children?: React.ReactNode;
}>;
state: Readonly<State<T>>;
refs: {
[key: string]: React.ReactInstance;
};
shouldComponentUpdate?(nextProps: Readonly<T>, nextState: Readonly<State<T>>, nextContext: any): boolean;
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
getSnapshotBeforeUpdate?(prevProps: Readonly<T>, prevState: Readonly<State<T>>): any;
componentDidUpdate?(prevProps: Readonly<T>, prevState: Readonly<State<T>>, snapshot?: any): void;
componentWillMount?(): void;
UNSAFE_componentWillMount?(): void;
componentWillReceiveProps?(nextProps: Readonly<T>, nextContext: any): void;
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<T>, nextContext: any): void;
componentWillUpdate?(nextProps: Readonly<T>, nextState: Readonly<State<T>>, nextContext: any): void;
UNSAFE_componentWillUpdate?(nextProps: Readonly<T>, nextState: Readonly<State<T>>, nextContext: any): void;
};
contextType?: React.Context<any>;
};
export default getAsyncComponent;