File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ import React , { Component } from 'react' ;
2+
3+ const errorLoading = ( err ) => console . error ( 'AsyncComponent: Loading failed' , err ) ;
4+ const isFunction = ( func ) => Object . prototype . toString . call ( func ) === '[object Function]' ;
5+
6+ export default ( loader , { placeholder } = { } ) => {
7+ class AsyncComponent extends Component {
8+ constructor ( ) {
9+ super ( ) ;
10+
11+ this . state = {
12+ component : null
13+ } ;
14+ this . mounting = true ;
15+ }
16+
17+ componentWillMount ( ) {
18+ if ( ! isFunction ( loader ) ) {
19+ return console . error ( 'AsyncComponent: Loader is not function' ) ;
20+ }
21+
22+ loader ( )
23+ . then ( ( module ) => this . mounting && this . setState ( { component : module . default } ) )
24+ . catch ( errorLoading ) ;
25+ }
26+
27+ componentWillUnmount ( ) {
28+ this . mounting = true ;
29+ }
30+
31+ render ( ) {
32+ if ( this . state . component ) {
33+ return < this . state . component { ...this . props } />
34+ }
35+
36+ return (
37+ placeholder || null
38+ ) ;
39+ }
40+ }
41+
42+ return AsyncComponent ;
43+ }
Original file line number Diff line number Diff line change 1+ import AsyncComponent from './AsyncComponent' ;
2+
3+ export {
4+ AsyncComponent
5+ }
You can’t perform that action at this time.
0 commit comments