Chap05 -> Lazy loading routers

Chap05
Jason Zhu 2022-03-30 17:05:28 +11:00
parent 3580e9b0b4
commit c913b32ddb
2 changed files with 23 additions and 2 deletions

View File

@ -7,12 +7,13 @@ import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { Header } from './Header'; import { Header } from './Header';
import { HomePage } from './HomePage'; import { HomePage } from './HomePage';
import { fontFamily, fontSize, gray2 } from './Styles'; import { fontFamily, fontSize, gray2 } from './Styles';
import { AskPage } from './AskPage';
import { SearchPage } from './SearchPage'; import { SearchPage } from './SearchPage';
import { SignInPage } from './SignInPage'; import { SignInPage } from './SignInPage';
import { NotFoundPage } from './NotFoundPage'; import { NotFoundPage } from './NotFoundPage';
import { QuestionPage } from './QuestionPage'; import { QuestionPage } from './QuestionPage';
const AskPage = React.lazy(() => import('./AskPage'));
function App() { function App() {
return ( return (
<BrowserRouter> <BrowserRouter>
@ -27,7 +28,25 @@ function App() {
<Routes> <Routes>
<Route path="" element={<HomePage />} /> <Route path="" element={<HomePage />} />
<Route path="search" element={<SearchPage />} /> <Route path="search" element={<SearchPage />} />
<Route path="ask" element={<AskPage />} /> <Route
path="ask"
element={
<React.Suspense
fallback={
<div
css={css`
margin-top: 100px;
text-align: center;
`}
>
Loading...
</div>
}
>
<AskPage />
</React.Suspense>
}
/>
<Route path="signin" element={<SignInPage />} /> <Route path="signin" element={<SignInPage />} />
<Route path="questions/:questionId" element={<QuestionPage />} /> <Route path="questions/:questionId" element={<QuestionPage />} />
<Route path="*" element={<NotFoundPage />} /> <Route path="*" element={<NotFoundPage />} />

View File

@ -2,3 +2,5 @@ import React from 'react';
import { Page } from './Page'; import { Page } from './Page';
export const AskPage = () => <Page title="Ask a question">{null}</Page>; export const AskPage = () => <Page title="Ask a question">{null}</Page>;
export default AskPage;