Skip to main content

11.30 Lazy Loading With React Suspense

App.js (Suspense branch in GitHub)

App.js
import React, { Component, Suspense } from "react";
import User from "./containers/User";

// import Posts from './containers/Posts';
const Posts = React.lazy(() => import("./containers/Posts"));

class App extends Component {
state = { showPosts: false };
modeHandler = () => {
this.setState((prevState) => {
return { showPosts: !prevState.showPosts };
});
};
render() {
return (
<React.Fragment>
<button onClick={this.modeHandler}>Toggle Mode</button>
{this.state.showPosts ? (
<Suspense fallback={<div>Loading...</div>}>
<Posts />
</Suspense>
) : (
<User />
)}
</React.Fragment>
// <BrowserRouter>
// <React.Fragment>
// <nav>
// <NavLink to="/user">User Page</NavLink> |&nbsp;
// <NavLink to="/posts">Posts Page</NavLink>
// </nav>
// <Route path="/" component={Welcome} exact />
// <Route path="/user" component={User} />
// <Route
// path="/posts"
// render={() => (
// <Suspense fallback={<div>Loading...</div>}>
// <Posts />
// </Suspense>
// )}
// />
// </React.Fragment>
// </BrowserRouter>
);
}
}