-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example(hackernews): use Code splitting and lazy loading (#23)
* fix: 修复 TypeScript 类型问题 * example: Resolved TypeScript type and Using Loaders to ensure data is loaded * example: Lock react@18 and Load the comment data with loader * ci: pnpm-lock.yaml-settings.autoInstallPeers * ci: pnpm-lock.yaml-settings.autoInstallPeers * Update examples/hackernews/tsconfig.json * refactor(hackernews): use Code splitting and lazy loading * refactor(hackernews): Encapsulating a route files into a directory --------- Co-authored-by: chencheng (云谦) <[email protected]>
- Loading branch information
Showing
27 changed files
with
199 additions
and
155 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemList from '../../components/item-list'; | ||
import type { FetchListResult } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/ask/$page')({ | ||
component: AskComponent, | ||
}); | ||
|
||
function AskComponent() { | ||
const { page } = Route.useParams(); | ||
const { items, maxPage } = Route.useLoaderData<FetchListResult>(); | ||
return ( | ||
<ItemList type="ask" page={Number(page)} maxPage={maxPage} items={items} /> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchList } from '../../services/api'; | ||
import type { Params } from '../../types'; | ||
|
||
export const Route = createFileRoute('/ask/$page')({ | ||
loader: async ({ params }: { params: Params }) => | ||
await fetchList('ask', Number(params.page)), | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemList from '../../components/item-list'; | ||
import type { FetchListResult, Params } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/')({ | ||
component: TopComponent, | ||
}); | ||
|
||
function TopComponent() { | ||
const params = Route.useParams<Params>(); | ||
const page = Number(params?.page ?? '1') ?? 1; | ||
const { items, maxPage } = Route.useLoaderData<FetchListResult>(); | ||
return <ItemList type="top" page={page} maxPage={maxPage} items={items} />; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchList } from '../../services/api'; | ||
import type { Params } from '../../types'; | ||
|
||
export const Route = createFileRoute('/')({ | ||
loader: async ({ params }: { params: Params }) => | ||
await fetchList('top', Number(params?.page ?? '1') ?? 1), | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemPage from '../../components/item-page'; | ||
import type { ItemIdInfo } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/item/$itemId')({ | ||
component: Item, | ||
}); | ||
|
||
function Item() { | ||
const { item, comments } = Route.useLoaderData<ItemIdInfo>(); | ||
return <ItemPage item={item} comments={comments} />; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchItem, fetchItems } from '../../services'; | ||
import type { CommentType, ItemIdInfo } from '../../types'; | ||
|
||
export const Route = createFileRoute('/item/$itemId')({ | ||
loader: async ({ | ||
params, | ||
}: { | ||
params: { itemId: string }; | ||
}): Promise<ItemIdInfo> => { | ||
const item = await fetchItem(params.itemId); | ||
const kids = item.kids || []; | ||
const comments: CommentType[] = await fetchItems(kids); | ||
return { item, comments }; | ||
}, | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemList from '../../components/item-list'; | ||
import type { FetchListResult } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/job/$page')({ | ||
component: JobComponent, | ||
}); | ||
|
||
function JobComponent() { | ||
const { page } = Route.useParams(); | ||
const { items, maxPage } = Route.useLoaderData<FetchListResult>(); | ||
return ( | ||
<ItemList type="job" page={Number(page)} maxPage={maxPage} items={items} /> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchList } from '../../services/api'; | ||
import type { Params } from '../../types'; | ||
|
||
export const Route = createFileRoute('/job/$page')({ | ||
loader: async ({ params }: { params: Params }) => | ||
await fetchList('job', Number(params.page)), | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemList from '../../components/item-list'; | ||
import type { FetchListResult } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/new/$page')({ | ||
component: NewComponent, | ||
}); | ||
|
||
function NewComponent() { | ||
const { page } = Route.useParams(); | ||
const { items, maxPage } = Route.useLoaderData<FetchListResult>(); | ||
return ( | ||
<ItemList type="new" page={Number(page)} maxPage={maxPage} items={items} /> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchList } from '../../services/api'; | ||
import type { Params } from '../../types'; | ||
|
||
export const Route = createFileRoute('/new/$page')({ | ||
loader: async ({ params }: { params: Params }) => | ||
await fetchList('new', Number(params.page)), | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemList from '../../components/item-list'; | ||
import type { FetchListResult } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/show/$page')({ | ||
component: ShowComponent, | ||
}); | ||
|
||
function ShowComponent() { | ||
const { page } = Route.useParams(); | ||
const { items, maxPage } = Route.useLoaderData<FetchListResult>(); | ||
return ( | ||
<ItemList type="show" page={Number(page)} maxPage={maxPage} items={items} /> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchList } from '../../services/api'; | ||
import type { Params } from '../../types'; | ||
|
||
export const Route = createFileRoute('/show/$page')({ | ||
loader: async ({ params }: { params: Params }) => | ||
await fetchList('show', Number(params.page)), | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import ItemList from '../../components/item-list'; | ||
import type { FetchListResult } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/top/$page')({ | ||
component: TopComponent, | ||
}); | ||
|
||
function TopComponent() { | ||
const { page } = Route.useParams(); | ||
const { items, maxPage } = Route.useLoaderData<FetchListResult>(); | ||
return ( | ||
<ItemList type="top" page={Number(page)} maxPage={maxPage} items={items} /> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchList } from '../../services/api'; | ||
import type { Params } from '../../types'; | ||
|
||
export const Route = createFileRoute('/top/$page')({ | ||
loader: async ({ params }: { params: Params }) => | ||
await fetchList('top', Number(params.page)), | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React, { createLazyFileRoute } from '@umijs/tnf/router'; | ||
import UserPage from '../../components/user-page'; | ||
import type { User } from '../../types'; | ||
|
||
export const Route = createLazyFileRoute('/user/$userId')({ | ||
component: User, | ||
}); | ||
|
||
function User() { | ||
const user = Route.useLoaderData<User>(); | ||
return <UserPage user={user} />; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createFileRoute } from '@umijs/tnf/router'; | ||
import { fetchUser } from '../../services'; | ||
|
||
export const Route = createFileRoute('/user/$userId')({ | ||
loader: async ({ params }: { params: { userId: string } }) => | ||
await fetchUser(params.userId), | ||
}); |
Oops, something went wrong.