Skip to content

Commit 972bb0f

Browse files
committed
Upgrading
1 parent 5d31812 commit 972bb0f

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/Routes.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import React from "react";
22
import { Route, Switch } from "react-router-dom";
3+
import asyncComponent from "./components/AsyncComponent";
34
import AppliedRoute from "./components/AppliedRoute";
45
import AuthenticatedRoute from "./components/AuthenticatedRoute";
56
import UnauthenticatedRoute from "./components/UnauthenticatedRoute";
67

7-
import Home from "./containers/Home";
8-
import Login from "./containers/Login";
9-
import Notes from "./containers/Notes";
10-
import Signup from "./containers/Signup";
11-
import NewNote from "./containers/NewNote";
12-
import NotFound from "./containers/NotFound";
8+
const AsyncHome = asyncComponent(() => import("./containers/Home"));
9+
const AsyncLogin = asyncComponent(() => import("./containers/Login"));
10+
const AsyncNotes = asyncComponent(() => import("./containers/Notes"));
11+
const AsyncSignup = asyncComponent(() => import("./containers/Signup"));
12+
const AsyncNewNote = asyncComponent(() => import("./containers/NewNote"));
13+
const AsyncNotFound = asyncComponent(() => import("./containers/NotFound"));
1314

1415
export default ({ childProps }) =>
1516
<Switch>
16-
<AppliedRoute path="/" exact component={Home} props={childProps} />
17-
18-
<UnauthenticatedRoute path="/login" exact component={Login} props={childProps} />
19-
<UnauthenticatedRoute path="/signup" exact component={Signup} props={childProps} />
20-
<AuthenticatedRoute path="/notes/:id" exact component={Notes} props={childProps} />
21-
<AuthenticatedRoute path="/notes/new" exact component={NewNote} props={childProps} />
17+
<AppliedRoute path="/" exact component={AsyncHome} props={childProps} />
18+
<UnauthenticatedRoute path="/login" exact component={AsyncLogin} props={childProps} />
19+
<UnauthenticatedRoute path="/signup" exact component={AsyncSignup} props={childProps} />
20+
<AuthenticatedRoute path="/notes/new" exact component={AsyncNewNote} props={childProps} />
21+
<AuthenticatedRoute path="/notes/:id" exact component={AsyncNotes} props={childProps} />
2222
{ /* Finally, catch all unmatched routes */ }
23-
<Route component={NotFound} />
23+
<Route component={AsyncNotFound} />
2424
</Switch>;

src/components/AsyncComponent.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { Component } from "react";
2+
3+
export default function asyncComponent(importComponent) {
4+
class AsyncComponent extends Component {
5+
constructor(props) {
6+
super(props);
7+
8+
this.state = {
9+
component: null
10+
};
11+
}
12+
13+
async componentDidMount() {
14+
const { default: component } = await importComponent();
15+
16+
this.setState({
17+
component: component
18+
});
19+
}
20+
21+
render() {
22+
const C = this.state.component;
23+
24+
return C ? <C {...this.props} /> : null;
25+
}
26+
}
27+
28+
return AsyncComponent;
29+
}

0 commit comments

Comments
 (0)