Skip to content

Commit eeca22d

Browse files
committed
refactor(hooks): remove executeOnMount functionality
BREAKING CHANGE: Remove the deprecated execute on mount functionality, found in hooks.
1 parent 8378848 commit eeca22d

File tree

8 files changed

+10
-79
lines changed

8 files changed

+10
-79
lines changed

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import {} from "react/experimental";
3-
import type { HookActionStatus, HookBaseUtils, HookCallbacks, HookShorthandStatus } from "./hooks.types";
3+
import type { HookActionStatus, HookCallbacks, HookShorthandStatus } from "./hooks.types";
44
import type { SafeActionResult } from "./index.types";
55
import type { InferInputOrDefault, StandardSchemaV1 } from "./standard.types";
66

@@ -52,27 +52,6 @@ export const getActionShorthandStatusObject = ({
5252
};
5353
};
5454

55-
export const useExecuteOnMount = <S extends StandardSchemaV1 | undefined>(
56-
args: HookBaseUtils<S> & {
57-
executeFn: (input: InferInputOrDefault<S, void>) => void;
58-
}
59-
) => {
60-
const mounted = React.useRef(false);
61-
62-
React.useEffect(() => {
63-
const t = setTimeout(() => {
64-
if (args.executeOnMount && !mounted.current) {
65-
args.executeFn(args.executeOnMount.input as InferInputOrDefault<S, void>);
66-
mounted.current = true;
67-
}
68-
}, args.executeOnMount?.delayMs ?? 0);
69-
70-
return () => {
71-
clearTimeout(t);
72-
};
73-
}, [args]);
74-
};
75-
7655
export const useActionCallbacks = <
7756
ServerError,
7857
S extends StandardSchemaV1 | undefined,

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import * as React from "react";
44
import {} from "react/experimental";
5-
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks, useExecuteOnMount } from "./hooks-utils";
5+
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks } from "./hooks-utils";
66
import type {
7-
HookBaseUtils,
87
HookCallbacks,
98
HookSafeActionFn,
109
UseActionHookReturn,
@@ -31,7 +30,7 @@ export const useAction = <
3130
Data,
3231
>(
3332
safeActionFn: HookSafeActionFn<ServerError, S, BAS, CVE, CBAVE, Data>,
34-
utils?: HookBaseUtils<S> & HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
33+
utils?: HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
3534
): UseActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data> => {
3635
const [isTransitioning, startTransition] = React.useTransition();
3736
const [result, setResult] = React.useState<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>({});
@@ -100,11 +99,6 @@ export const useAction = <
10099
setResult({});
101100
}, []);
102101

103-
useExecuteOnMount({
104-
executeOnMount: utils?.executeOnMount,
105-
executeFn: execute,
106-
});
107-
108102
useActionCallbacks({
109103
result: result ?? {},
110104
input: clientInput as InferInputOrDefault<S, undefined>,
@@ -148,8 +142,7 @@ export const useOptimisticAction = <
148142
utils: {
149143
currentState: State;
150144
updateFn: (state: State, input: InferInputOrDefault<S, void>) => State;
151-
} & HookBaseUtils<S> &
152-
HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
145+
} & HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
153146
): UseOptimisticActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data, State> => {
154147
const [isTransitioning, startTransition] = React.useTransition();
155148
const [result, setResult] = React.useState<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>({});
@@ -224,11 +217,6 @@ export const useOptimisticAction = <
224217
setResult({});
225218
}, []);
226219

227-
useExecuteOnMount({
228-
executeOnMount: utils?.executeOnMount,
229-
executeFn: execute,
230-
});
231-
232220
useActionCallbacks({
233221
result: result ?? {},
234222
input: clientInput as InferInputOrDefault<S, undefined>,

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@ import type { SafeActionFn, SafeActionResult, SafeStateActionFn } from "./index.
22
import type { InferInputOrDefault, StandardSchemaV1 } from "./standard.types";
33
import type { MaybePromise, Prettify } from "./utils.types";
44

5-
/**
6-
* Type of base utils object passed to `useAction`, `useOptimisticAction` and `useStateAction` hooks.
7-
*/
8-
export type HookBaseUtils<S extends StandardSchemaV1 | undefined> = {
9-
/**
10-
* @deprecated Actions should not execute on component mount, since they're used to mutate data.
11-
*/
12-
executeOnMount?: (undefined extends S
13-
? { input?: undefined }
14-
: {
15-
input: InferInputOrDefault<S, undefined>;
16-
}) & { delayMs?: number };
17-
};
18-
195
/**
206
* Type of hooks callbacks. These are executed when action is in a specific state.
217
*/

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import * as React from "react";
44
import {} from "react/experimental";
5-
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks, useExecuteOnMount } from "./hooks-utils";
6-
import type { HookBaseUtils, HookCallbacks, HookSafeStateActionFn, UseStateActionHookReturn } from "./hooks.types";
5+
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks } from "./hooks-utils";
6+
import type { HookCallbacks, HookSafeStateActionFn, UseStateActionHookReturn } from "./hooks.types";
77
import type { InferInputOrDefault, StandardSchemaV1 } from "./standard.types";
88

99
/**
@@ -25,8 +25,7 @@ export const useStateAction = <
2525
utils?: {
2626
initResult?: Awaited<ReturnType<typeof safeActionFn>>;
2727
permalink?: string;
28-
} & HookBaseUtils<S> &
29-
HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
28+
} & HookCallbacks<ServerError, S, BAS, CVE, CBAVE, Data>
3029
): UseStateActionHookReturn<ServerError, S, BAS, CVE, CBAVE, Data> => {
3130
const [result, dispatcher, isExecuting] = React.useActionState(
3231
safeActionFn,
@@ -56,11 +55,6 @@ export const useStateAction = <
5655
[dispatcher]
5756
);
5857

59-
useExecuteOnMount({
60-
executeOnMount: utils?.executeOnMount,
61-
executeFn: execute,
62-
});
63-
6458
useActionCallbacks({
6559
result: result ?? {},
6660
input: clientInput as InferInputOrDefault<S, undefined>,

website/docs/execute-actions/hooks/hook-base-utils.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

website/docs/execute-actions/hooks/useaction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ As you can see, here we display a greet message after the action is performed, i
6464
### `useAction` arguments
6565

6666
- `safeActionFn`: the safe action that will be called via `execute` or `executeAsync` functions.
67-
- `utils?`: object with optional [base utils](/docs/execute-actions/hooks/hook-base-utils) and [callbacks](/docs/execute-actions/hooks/hook-callbacks).
67+
- `utils?`: object with [callbacks](/docs/execute-actions/hooks/hook-callbacks).
6868

6969
### `useAction` return object
7070

website/docs/execute-actions/hooks/useoptimisticaction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export default function TodosBox({ todos }: Props) {
117117
### `useOptimisticAction` arguments
118118

119119
- `safeActionFn`: the safe action that will be called via `execute` or `executeAsync` functions.
120-
- `utils`: object with required `currentState` and `updateFn` properties and optional [base utils](/docs/execute-actions/hooks/hook-base-utils) and [callbacks](/docs/execute-actions/hooks/hook-callbacks). `currentState` is passed from the parent Server Component, and `updateFn` tells the hook how to update the optimistic state before receiving the server response.
120+
- `utils`: object with required `currentState` and `updateFn` properties and optional [callbacks](/docs/execute-actions/hooks/hook-callbacks). `currentState` is passed from the parent Server Component, and `updateFn` tells the hook how to update the optimistic state before receiving the server response.
121121

122122
### `useOptimisticAction` return object
123123

website/docs/execute-actions/hooks/usestateaction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default function StatefulFormPage() {
9292
### `useStateAction` arguments
9393

9494
- `safeActionFn`: the safe stateful action that will be called via `execute` or `executeAsync` functions.
95-
- `utils`: object with optional `initResult`, `permalink`, [base utils](/docs/execute-actions/hooks/hook-base-utils) and [callbacks](/docs/execute-actions/hooks/hook-callbacks) properties. `initResult` is used to define the initial state of the stateful action. If not passed, the initial state will default to an empty object: `{}`. `permalink` is documented in [React docs](https://react.dev/reference/react/useActionState#parameters) for `useActionState` hook.
95+
- `utils`: object with optional `initResult`, `permalink`, and [callbacks](/docs/execute-actions/hooks/hook-callbacks) properties. `initResult` is used to define the initial state of the stateful action. If not passed, the initial state will default to an empty object: `{}`. `permalink` is documented in [React docs](https://react.dev/reference/react/useActionState#parameters) for `useActionState` hook.
9696

9797
### `useStateAction` return object
9898

0 commit comments

Comments
 (0)