File tree 7 files changed +38
-410
lines changed
7 files changed +38
-410
lines changed Original file line number Diff line number Diff line change 1
- import React , { PureComponent } from 'react' ;
2
- import PropTypes from 'prop-types' ;
3
-
4
- class AsyncComponent extends PureComponent {
5
-
6
- constructor ( props ) {
7
- super ( props ) ;
8
-
9
- this . state = {
10
- Component : null
11
- } ;
12
- }
13
-
14
- unCamelCase ( s ) {
15
- return s . split ( / (? = [ A - Z ] ) / ) . join ( '-' ) . toLowerCase ( ) ;
16
- }
17
-
18
- componentWillMount ( ) {
19
- if ( ! this . state . Component ) {
20
- this . props . moduleProvider ( ) . then ( ( component ) => {
21
- document . body . setAttribute ( 'page' , this . unCamelCase ( component . default . name ) ) ;
1
+ import React from 'react' ;
2
+
3
+
4
+ const AsyncComponent = ( importComponent ) => {
5
+
6
+ return class Component extends React . Component {
7
+
8
+ state = {
9
+ component : null ,
10
+ }
11
+
12
+ componentDidMount ( ) {
13
+
14
+ importComponent ( ) . then ( ( component ) => {
22
15
this . setState ( { component : component . default } ) ;
23
16
} ) ;
17
+
18
+ }
19
+
20
+ render ( ) {
21
+ const Component = this . state . component ;
22
+
23
+ return ( Component ) ? (
24
+ < React . Fragment >
25
+ { Component ? < Component { ...this . props } /> : null }
26
+ </ React . Fragment >
27
+ ) : (
28
+ < React . Fragment >
29
+ < p > Загрузка...</ p >
30
+ </ React . Fragment >
31
+ ) ;
32
+
24
33
}
25
- }
26
-
27
- componentWillUnmount ( ) {
28
- document . body . removeAttribute ( 'page' ) ;
29
- }
30
-
31
- render ( ) {
32
- const Component = this . state . component ;
33
- return (
34
- < React . Fragment >
35
- { /* {Component ? <Component {...this.props} /> : null } */ }
36
- { Component ? < Component /> : null }
37
- </ React . Fragment >
38
- ) ;
39
- }
40
- }
41
-
42
- AsyncComponent . propTypes = {
43
- moduleProvider : PropTypes . func
34
+ } ;
44
35
} ;
45
36
37
+
46
38
export default AsyncComponent ;
Original file line number Diff line number Diff line change
1
+ < div id ="app "> </ div >
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -2,16 +2,16 @@ import React from 'react';
2
2
import { Switch , Route } from 'react-router-dom' ;
3
3
import AsyncComponent from '../commons/AsyncComponent' ;
4
4
5
- const Home = ( ) => import ( './home/Home' ) ;
6
- const About = ( ) => import ( './about/About' ) ;
5
+ const Home = AsyncComponent ( ( ) => import ( './home/Home' ) ) ;
6
+ const About = AsyncComponent ( ( ) => import ( './about/About' ) ) ;
7
7
8
8
class Pages extends React . Component {
9
9
render ( ) {
10
10
return (
11
11
< main >
12
12
< Switch >
13
- < Route exact path = '/' component = { ( ) => < AsyncComponent moduleProvider = { Home } /> } />
14
- < Route path = '/about' component = { ( ) => < AsyncComponent moduleProvider = { About } /> } />
13
+ < Route exact path = '/' component = { Home } />
14
+ < Route path = '/about' component = { About } />
15
15
</ Switch >
16
16
</ main >
17
17
) ;
You can’t perform that action at this time.
0 commit comments