An example React router implementation and demo of lazily loading components with React.lazy and Suspense
An example React router implementation and demo of lazily loading components with React.lazy
and Suspense.
import * as React from 'react';
import Nav from './Nav.jsx';
import { Router } from './routing';
const routes = new Map<string, React.ComponentType>([
['/', () => <p>Pick an Ipsum!</p>],
['/lorem', React.lazy(() => import('./pages/Lorem'))],
['/bacon', React.lazy(() => import('./pages/Bacon'))],
['/hipster', React.lazy(() => import('./pages/Hipster'))],
['/office', React.lazy(() => import('./pages/Office'))],
]);
const paths = [...routes.keys(), '/missing'].slice(1);
const App = () => (
<Router routes={routes} initialPath="/" notFound={<p>Route not found</p>}>
{Page => (
<>
<Nav paths={paths} />
<React.Suspense fallback={<div className="loading-spinner" />}>
<Page />
</React.Suspense>
</>
)}
</Router>
);
TODO
To set up:
git clone https://github.com/jamesseanwright/react-lazy-routes.git
cd react-lazy-routes
npm i
nvm use
Then you can run one of the following commands:
npm run dev
- builds the project with rollup.js and serves it from port 8080npm test
- runs the unit tests (append -- --watch
to launch Jest's watch mode)