Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
34af4197
Unverified
Commit
34af4197
authored
Aug 14, 2026
by
Aurora Scharff
Committed by
GitHub
Aug 14, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docs: align SPA guide reducer example with React conventions (#97341)
parent
a4420747
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
24 deletions
+18
-24
single-page-applications.mdx
docs/01-app/02-guides/single-page-applications.mdx
+18
-24
No files found.
docs/01-app/02-guides/single-page-applications.mdx
View file @
34af4197
...
@@ -326,7 +326,7 @@ export function DeletePost({ id }) {
...
@@ -326,7 +326,7 @@ export function DeletePost({ id }) {
For
list
-
like
state
where
each
change
should
appear
instantly
,
you
can
combine
`useActionState`
with
`useOptimistic`
.
The
example
below
is
a
to
-
do
list
:
a
pure
reducer
defines
how
each
action
changes
the
list
,
so
the
client
and
server
share
one
copy
of
that
logic
:
For
list
-
like
state
where
each
change
should
appear
instantly
,
you
can
combine
`useActionState`
with
`useOptimistic`
.
The
example
below
is
a
to
-
do
list
:
a
pure
reducer
defines
how
each
action
changes
the
list
,
so
the
client
and
server
share
one
copy
of
that
logic
:
```ts filename="app/reducer.ts" switcher
```ts filename="app/
todos-
reducer.ts" switcher
export type Todo = { id: string; text: string; done: boolean }
export type Todo = { id: string; text: string; done: boolean }
export type TodoAction =
export type TodoAction =
...
@@ -335,7 +335,7 @@ export type TodoAction =
...
@@ -335,7 +335,7 @@ export type TodoAction =
| { type: 'edit'; id: string; text: string }
| { type: 'edit'; id: string; text: string }
| { type: 'delete'; id: string }
| { type: 'delete'; id: string }
export function
applyAction
(todos: Todo[], action: TodoAction): Todo[] {
export function
todosReducer
(todos: Todo[], action: TodoAction): Todo[] {
switch (action.type) {
switch (action.type) {
case 'add':
case 'add':
return [...todos, { id: action.id, text: action.text, done: false }]
return [...todos, { id: action.id, text: action.text, done: false }]
...
@@ -355,8 +355,8 @@ export function applyAction(todos: Todo[], action: TodoAction): Todo[] {
...
@@ -355,8 +355,8 @@ export function applyAction(todos: Todo[], action: TodoAction): Todo[] {
}
}
```
```
```js filename="app/reducer.js" switcher
```js filename="app/
todos-
reducer.js" switcher
export function
applyAction
(todos, action) {
export function
todosReducer
(todos, action) {
switch (action.type) {
switch (action.type) {
case 'add':
case 'add':
return [...todos, { id: action.id, text: action.text, done: false }]
return [...todos, { id: action.id, text: action.text, done: false }]
...
@@ -382,13 +382,13 @@ The Server Action applies the reducer, persists the result, and returns the next
...
@@ -382,13 +382,13 @@ The Server Action applies the reducer, persists the result, and returns the next
'use server'
'use server'
import { db } from './db'
import { db } from './db'
import {
applyAction, type Todo, type TodoAction } from './
reducer'
import {
todosReducer, type Todo, type TodoAction } from './todos-
reducer'
export async function
todosReducer
(
export async function
saveTodos
(
todos: Todo[],
todos: Todo[],
action: TodoAction
action: TodoAction
): Promise<Todo[]> {
): Promise<Todo[]> {
const next =
applyAction
(todos, action)
const next =
todosReducer
(todos, action)
await db.saveTodos(next)
await db.saveTodos(next)
return next
return next
}
}
...
@@ -398,10 +398,10 @@ export async function todosReducer(
...
@@ -398,10 +398,10 @@ export async function todosReducer(
'use server'
'use server'
import { db } from './db'
import { db } from './db'
import {
applyAction } from './
reducer'
import {
todosReducer } from './todos-
reducer'
export async function
todosReducer
(todos, action) {
export async function
saveTodos
(todos, action) {
const next =
applyAction
(todos, action)
const next =
todosReducer
(todos, action)
await db.saveTodos(next)
await db.saveTodos(next)
return next
return next
}
}
...
@@ -413,15 +413,12 @@ The client passes the same reducer to `useOptimistic`, so the optimistic update
...
@@ -413,15 +413,12 @@ The client passes the same reducer to `useOptimistic`, so the optimistic update
'use client'
'use client'
import { useActionState, useOptimistic, startTransition } from 'react'
import { useActionState, useOptimistic, startTransition } from 'react'
import {
todosReducer
} from './actions'
import {
saveTodos
} from './actions'
import {
applyAction, type Todo, type TodoAction } from './
reducer'
import {
todosReducer, type Todo, type TodoAction } from './todos-
reducer'
export function TodoList({ initialTodos }: { initialTodos: Todo[] }) {
export function TodoList({ initialTodos }: { initialTodos: Todo[] }) {
const [todos, dispatch, isPending] = useActionState(
const [todos, dispatch, isPending] = useActionState(saveTodos, initialTodos)
todosReducer,
const [optimisticTodos, addOptimistic] = useOptimistic(todos, todosReducer)
initialTodos
)
const [optimisticTodos, addOptimistic] = useOptimistic(todos, applyAction)
function runAction(action: TodoAction) {
function runAction(action: TodoAction) {
startTransition(() => {
startTransition(() => {
...
@@ -473,15 +470,12 @@ export function TodoList({ initialTodos }: { initialTodos: Todo[] }) {
...
@@ -473,15 +470,12 @@ export function TodoList({ initialTodos }: { initialTodos: Todo[] }) {
'use client'
'use client'
import { useActionState, useOptimistic, startTransition } from 'react'
import { useActionState, useOptimistic, startTransition } from 'react'
import {
todosReducer
} from './actions'
import {
saveTodos
} from './actions'
import {
applyAction } from './
reducer'
import {
todosReducer } from './todos-
reducer'
export function TodoList({ initialTodos }) {
export function TodoList({ initialTodos }) {
const [todos, dispatch, isPending] = useActionState(
const [todos, dispatch, isPending] = useActionState(saveTodos, initialTodos)
todosReducer,
const [optimisticTodos, addOptimistic] = useOptimistic(todos, todosReducer)
initialTodos
)
const [optimisticTodos, addOptimistic] = useOptimistic(todos, applyAction)
function runAction(action) {
function runAction(action) {
startTransition(() => {
startTransition(() => {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment