Skip to content

Commit 7b7e2a1

Browse files
async component
1 parent f988ca9 commit 7b7e2a1

File tree

7 files changed

+38
-410
lines changed

7 files changed

+38
-410
lines changed

app/commons/AsyncComponent.jsx

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
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) => {
2215
this.setState({ component: component.default });
2316
});
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+
2433
}
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+
};
4435
};
4536

37+
4638
export default AsyncComponent;

app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="app"></div>

app/index.pug

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/pages/Pages.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import React from 'react';
22
import { Switch, Route } from 'react-router-dom';
33
import AsyncComponent from '../commons/AsyncComponent';
44

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'));
77

88
class Pages extends React.Component {
99
render() {
1010
return (
1111
<main>
1212
<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} />
1515
</Switch>
1616
</main>
1717
);

0 commit comments

Comments
 (0)