diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-28 00:05:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-28 00:05:47 +0800 |
commit | 48b3d8526560d389e74beb12bbd64b7be6e9268f (patch) | |
tree | 00ae6e24314793cd303b154ede4fe4f55c654e84 /packages/website/ts/lazy_component.tsx | |
parent | b5ce876327fe6443364837ea65cf28ec0e371949 (diff) | |
parent | ecfee00feca331ee1efa55165471d79774cb03d2 (diff) | |
download | dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.tar dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.tar.gz dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.tar.bz2 dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.tar.lz dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.tar.xz dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.tar.zst dexon-sol-tools-48b3d8526560d389e74beb12bbd64b7be6e9268f.zip |
Merge pull request #237 from 0xProject/addWebsite
Add Website to Mono Repo
Diffstat (limited to 'packages/website/ts/lazy_component.tsx')
-rw-r--r-- | packages/website/ts/lazy_component.tsx | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/packages/website/ts/lazy_component.tsx b/packages/website/ts/lazy_component.tsx new file mode 100644 index 000000000..7052b7be6 --- /dev/null +++ b/packages/website/ts/lazy_component.tsx @@ -0,0 +1,66 @@ +import * as React from 'react'; +import * as _ from 'lodash'; + +interface LazyComponentProps { + reactComponentPromise: Promise<React.ComponentClass<any>>; + reactComponentProps: any; +} + +interface LazyComponentState { + component?: React.ComponentClass<any>; +} + +/** + * This component is used for rendering components that are lazily loaded from other chunks. + * Source: https://reacttraining.com/react-router/web/guides/code-splitting + */ +export class LazyComponent extends React.Component<LazyComponentProps, LazyComponentState> { + constructor(props: LazyComponentProps) { + super(props); + this.state = { + component: undefined, + }; + } + public componentWillMount() { + this.loadComponentFireAndForgetAsync(this.props); + } + public componentWillReceiveProps(nextProps: LazyComponentProps) { + if (nextProps.reactComponentPromise !== this.props.reactComponentPromise) { + this.loadComponentFireAndForgetAsync(nextProps); + } + } + public render() { + return _.isUndefined(this.state.component) ? + null : + React.createElement(this.state.component, this.props.reactComponentProps); + } + private async loadComponentFireAndForgetAsync(props: LazyComponentProps) { + const component = await props.reactComponentPromise; + this.setState({ + component, + }); + } +} + +/** + * [createLazyComponent description] + * @param componentName name of exported component + * @param lazyImport lambda returning module promise + * we pass a lambda because we only want to require a module if it's used + * @example `const LazyPortal = createLazyComponent('Portal', () => System.import<any>('ts/containers/portal'));`` + */ +export const createLazyComponent = (componentName: string, lazyImport: () => Promise<any>) => { + return (props: any) => { + const reactComponentPromise = (async (): Promise<React.ComponentClass<any>> => { + const mod = await lazyImport(); + const component = mod[componentName]; + return component; + })(); + return ( + <LazyComponent + reactComponentPromise={reactComponentPromise} + reactComponentProps={props} + /> + ); + }; +}; |