Skip to main content

11.29 Loading Routes Lazily

New asyncComponent.js

asyncComponent.js
import { Component } from "react";

const asyncComponent = (importComponent) => {
return class extends Component {
state = {
component: null,
};

componentDidMount() {
importComponent().then((cmp) => {
this.setState({ component: cmp.default });
});
}
render() {
const C = this.state.component;

return C ? <C {...this.props} /> : null;
}
};
};

export default asyncComponent;

Blog.js

Blog.js
// import axios from "axios";
import React, { Component } from "react";
import { NavLink, Route, Switch } from "react-router-dom";
import asyncComponent from "../../hoc/asyncComponent";
import "./Blog.css";
// import NewPost from "./NewPost/NewPost";
import Posts from "./Posts/Posts";

const AsyncNewPost = asyncComponent(() => {
return import("./NewPost/NewPost");
});
class Blog extends Component {
state = {
auth: true,
};
render() {
return (
<div className="Blog">
<header>
<nav>
<ul>
<li>
<NavLink
to="/posts/"
exact
activeClassName="my-active"
activeStyle={{
color: "#fa923f",
textDecoration: "underline",
}}
>
Posts
</NavLink>
</li>
<li>
<NavLink
to={{
pathname: "/new-post",
hash: "#submit",
search: "?quick-submit=true",
}}
>
New Post
</NavLink>
</li>
</ul>
</nav>
</header>
{/* <Route path="/" exact render={() => <h1>Home</h1>} />
<Route path="/" render={() => <h1>Home2</h1>} /> */}

<Switch>
{this.state.auth ? (
<Route path="/new-post" component={AsyncNewPost} />
) : null}

<Route path="/posts" component={Posts} />
<Route render={() => <h1>Not found</h1>} />
{/* <Redirect from="/" to="/posts" /> */}
{/* <Route path="/" component={Posts} /> */}
</Switch>
</div>
);
}
}

export default Blog;