Created
June 10, 2019 18:58
-
-
Save NiallJoeMaher/74c122ac9d1a0d9820dd127d8158b53d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before | |
import React, { Component, Suspense, lazy } from "react"; | |
import ReactDOM from "react-dom"; | |
import { BrowserRouter as Router, Route, Link } from "react-router-dom"; | |
import Home from './Home' | |
import Topics from './Topics' | |
import Settings from './Settings' | |
class App extends Component { | |
render() { | |
return ( | |
<Router> | |
<div> | |
<ul> | |
<li><Link to='/'>Home</Link></li> | |
<li><Link to='/topics'>Topics</Link></li> | |
<li><Link to='/settings'>Settings</Link></li> | |
</ul> | |
<hr /> | |
<Route exact path='/' component={Home} /> | |
<Route path='/topics' component={Topics} /> | |
<Route path='/settings' component={Settings} /> | |
</div> | |
</Router> | |
) | |
} | |
} | |
export default App | |
// After | |
import React, { Component, Suspense, lazy } from "react"; | |
import ReactDOM from "react-dom"; | |
import { BrowserRouter as Router, Route, Link } from "react-router-dom"; | |
// Wrap each imported route in lazy import | |
const Home = lazy(() => import("./Home")); | |
const Topics = lazy(() => import("./Topics")); | |
const Settings = lazy(() => import("./Settings")); | |
class App extends Component { | |
render() { | |
return ( | |
<Router> | |
<div> | |
<ul> | |
<li> | |
<Link to="/">Home</Link> | |
</li> | |
<li> | |
<Link to="/topics">Topics</Link> | |
</li> | |
<li> | |
<Link to="/settings">Settings</Link> | |
</li> | |
</ul> | |
<hr /> | |
{/* Add Suspense around the routes so that whichever route that is loading it will show a loader */} | |
<Suspense fallback={<div>Loading...</div>}> | |
<Route exact path="/" component={Home} /> | |
<Route path="/topics" component={Topics} /> | |
<Route path="/settings" component={Settings} /> | |
</Suspense> | |
</div> | |
</Router> | |
); | |
} | |
} | |
export default App; | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment