Unverified Commit 34af4197 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

docs: align SPA guide reducer example with React conventions (#97341)

parent a4420747
...@@ -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(() => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment