Skip to content

Commit 686205a

Browse files
committed
lazy load comments
1 parent 7df80d5 commit 686205a

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

examples/lazy-injection/kitchen-sink/src/App.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const App = () => {
2323
<summary onClick={() => startTransition(toggleCounter)}>
2424
Counter example (lazy)
2525
</summary>
26-
<Suspense>{counterOpen && <Counter />}</Suspense>
26+
<Suspense fallback="Loading counter">
27+
{counterOpen && <Counter />}
28+
</Suspense>
2729
</details>
2830
<p>
2931
Edit <code>src/App.tsx</code> and save to reload.
@@ -32,7 +34,9 @@ const App = () => {
3234
<summary onClick={() => startTransition(toggleQuotes)}>
3335
Quotes example (lazy)
3436
</summary>
35-
<Suspense>{quotesOpen && <Quotes />}</Suspense>
37+
<Suspense fallback="Loading quotes">
38+
{quotesOpen && <Quotes />}
39+
</Suspense>
3640
</details>
3741
<span>
3842
<span>Learn </span>
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { combineSlices } from "@reduxjs/toolkit"
22
import { todoSlice } from "../features/todos/todoSlice"
3-
import { commentSlice } from "../features/todos/commentSlice"
43

54
export interface LazyLoadedSlices {}
65

76
// `combineSlices` automatically combines the reducers using
87
// their `reducerPath`s, therefore we no longer need to call `combineReducers`.
9-
export const rootReducer = combineSlices(
10-
todoSlice,
11-
commentSlice,
12-
).withLazyLoadedSlices<LazyLoadedSlices>()
8+
export const rootReducer =
9+
combineSlices(todoSlice).withLazyLoadedSlices<LazyLoadedSlices>()
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { useAppSelector } from "../../app/hooks"
22
import { AddTodo } from "./AddTodo"
3-
import { Todo } from "./Todo"
43
import { selectTodoIds } from "./todoSlice"
54
import styles from "./Todos.module.css"
5+
import { lazily } from "react-lazily"
6+
import { Suspense } from "react"
7+
8+
const { Todo } = lazily(() => import("./Todo"))
69

710
export function Todos() {
811
const todoIds = useAppSelector(selectTodoIds)
912
return (
1013
<div className={styles.todos}>
1114
<AddTodo />
12-
<div className={styles.todosList}>
13-
{todoIds.map(id => (
14-
<Todo key={id} id={id} />
15-
))}
16-
</div>
15+
<Suspense fallback="Loading todos">
16+
<div className={styles.todosList}>
17+
{todoIds.map(id => (
18+
<Todo key={id} id={id} />
19+
))}
20+
</div>
21+
</Suspense>
1722
</div>
1823
)
1924
}

examples/lazy-injection/kitchen-sink/src/features/todos/commentSlice.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { WithSlice } from "@reduxjs/toolkit"
12
import { createEntityAdapter, createSelector, nanoid } from "@reduxjs/toolkit"
23
import { createAppSlice } from "../../app/createAppSlice"
34
import { deleteTodo } from "./todoSlice"
5+
import { rootReducer } from "../../app/reducer"
46

57
export interface Comment {
68
id: string
@@ -49,11 +51,17 @@ export const commentSlice = createAppSlice({
4951

5052
export const { addComment, deleteComment } = commentSlice.actions
5153

54+
declare module "../../app/reducer" {
55+
interface LazyLoadedSlices extends WithSlice<typeof commentSlice> {}
56+
}
57+
58+
const injectedCommentSlice = commentSlice.injectInto(rootReducer)
59+
5260
export const {
5361
selectAll: selectAllComments,
5462
selectById: selectCommentById,
5563
selectEntities: selectCommentEntities,
5664
selectIds: selectCommentIds,
5765
selectTotal: selectCommentTotal,
5866
selectCommentsByTodoId,
59-
} = commentSlice.selectors
67+
} = injectedCommentSlice.selectors

0 commit comments

Comments
 (0)