Skip to content

Commit 131f8bf

Browse files
committedMay 19, 2024··
refactor(hooks): better useOptimisticAction variable names
1 parent 4ef5f9a commit 131f8bf

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed
 

‎apps/playground/src/app/(examples)/optimistic-hook/addtodo-form.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const AddTodoForm = ({ todos }: Props) => {
1515
const { execute, result, status, reset, optimisticState } =
1616
useOptimisticAction(addTodo, {
1717
currentState: { todos },
18-
updateFn: (prevState, newTodo) => ({
19-
todos: [...prevState.todos, newTodo],
18+
updateFn: (state, newTodo) => ({
19+
todos: [...state.todos, newTodo],
2020
}),
2121
onSuccess({ data, input }) {
2222
console.log("HELLO FROM ONSUCCESS", data, input);

‎packages/next-safe-action/src/hooks.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,15 @@ export const useOptimisticAction = <
210210
safeActionFn: HookSafeActionFn<ServerError, S, BAS, FVE, FBAVE, Data>,
211211
utils: {
212212
currentState: State;
213-
updateFn: (prevState: State, input: S extends Schema ? InferIn<S> : undefined) => State;
213+
updateFn: (state: State, input: S extends Schema ? InferIn<S> : undefined) => State;
214214
} & HookCallbacks<ServerError, S, BAS, FVE, FBAVE, Data>
215215
) => {
216216
const [, startTransition] = React.useTransition();
217217
const [result, setResult] = React.useState<HookResult<ServerError, S, BAS, FVE, FBAVE, Data>>(EMPTY_HOOK_RESULT);
218218
const [clientInput, setClientInput] = React.useState<S extends Schema ? InferIn<S> : void>();
219219
const [isExecuting, setIsExecuting] = React.useState(false);
220220
const [isIdle, setIsIdle] = React.useState(true);
221-
const [optimisticState, setOptimisticState] = React.useOptimistic<State, S extends Schema ? InferIn<S> : undefined>(
221+
const [optimisticState, setOptimisticValue] = React.useOptimistic<State, S extends Schema ? InferIn<S> : undefined>(
222222
utils.currentState,
223223
utils.updateFn
224224
);
@@ -232,7 +232,7 @@ export const useOptimisticAction = <
232232
setIsExecuting(true);
233233

234234
return startTransition(() => {
235-
setOptimisticState(input as S extends Schema ? InferIn<S> : undefined);
235+
setOptimisticValue(input as S extends Schema ? InferIn<S> : undefined);
236236
return safeActionFn(input as S extends Schema ? InferIn<S> : undefined)
237237
.then((res) => setResult(res ?? EMPTY_HOOK_RESULT))
238238
.catch((e) => {
@@ -247,7 +247,7 @@ export const useOptimisticAction = <
247247
});
248248
});
249249
},
250-
[safeActionFn, setOptimisticState]
250+
[safeActionFn, setOptimisticValue]
251251
);
252252

253253
const reset = () => {

‎website/docs/execution/hooks/useoptimisticaction.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ export default function TodosBox({ todos }: Props) {
8686
addTodo,
8787
{
8888
currentState: { todos }, // gets passed from Server Component
89-
updateFn: (prevState, newTodo) => {
89+
updateFn: (state, newTodo) => {
9090
return {
91-
todos: [...prevState.todos, newTodo]
91+
todos: [...state.todos, newTodo]
9292
};
9393
}
9494
}
@@ -118,14 +118,14 @@ export default function TodosBox({ todos }: Props) {
118118
| Name | Type | Purpose |
119119
| ----------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
120120
| `safeActionFn` | [`HookSafeActionFn`](/docs/types#hooksafeactionfn) | This is the action that will be called when you use `execute` from hook's return object. |
121-
| `utils` | `{ currentState: State; updateFn: (prevState: State, input: InferIn<S>) => State }` `&` [`HookCallbacks`](/docs/types#hookcallbacks) | Object with required `currentState`, `updateFn` and optional callbacks. See below for more information. |
121+
| `utils` | `{ currentState: State; updateFn: (state: State, input: InferIn<S>) => State }` `&` [`HookCallbacks`](/docs/types#hookcallbacks) | Object with required `currentState`, `updateFn` and optional callbacks. See below for more information. |
122122

123123
`utils` properties in detail:
124124

125125
| Name | Type | Purpose |
126126
| ----------------------- | ----------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
127127
| `currentState` | `State` (generic) | An optimistic state setter. This value should come from the parent Server Component. |
128-
| `updateFn` | `(prevState: State, input: InferIn<S>) => State` | When you call the action via `execute`, this function determines how the optimistic state update is performed. Basically, here you define what happens **immediately** after `execute` is called, and before the actual result comes back from the server (after revalidation). |
128+
| `updateFn` | `(state: State, input: InferIn<S>) => State` | When you call the action via `execute`, this function determines how the optimistic state update is performed. Basically, here you define what happens **immediately** after `execute` is called, and before the actual result comes back from the server (after revalidation). |
129129
| `{ onExecute?, onSuccess?, onError?, onSettled? }` | [`HookCallbacks`](/docs/types#hookcallbacks) | Optional callbacks. More information about them [here](/docs/execution/hooks/callbacks). |
130130

131131
### `useOptimisticAction` return object

0 commit comments

Comments
 (0)
Please sign in to comment.