Skip to content

Commit 89d0e70

Browse files
committed
refactor(hooks): removal of isPending shorthand status property
1 parent 05fc2c5 commit 89d0e70

File tree

11 files changed

+49
-47
lines changed

11 files changed

+49
-47
lines changed

apps/playground/src/app/(examples)/hook/page.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default function Hook() {
1818
isIdle,
1919
isExecuting,
2020
isTransitioning,
21-
isPending,
2221
hasSucceeded,
2322
hasErrored,
2423
} = useAction(deleteUser, {
@@ -44,7 +43,6 @@ export default function Hook() {
4443
isIdle,
4544
isExecuting,
4645
isTransitioning,
47-
isPending,
4846
hasSucceeded,
4947
hasErrored,
5048
});
@@ -53,7 +51,7 @@ export default function Hook() {
5351
<main className="w-96 max-w-full px-4">
5452
<StyledHeading>Action using hook</StyledHeading>
5553
<form
56-
className="flex flex-col mt-8 space-y-4"
54+
className="mt-8 flex flex-col space-y-4"
5755
onSubmit={async (e) => {
5856
e.preventDefault();
5957
const formData = new FormData(e.currentTarget);

apps/playground/src/app/(examples)/navigation/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default function Navigation() {
1717
isIdle,
1818
isExecuting,
1919
isTransitioning,
20-
isPending,
2120
hasSucceeded,
2221
hasErrored,
2322
} = useAction(testNavigate, {
@@ -44,7 +43,6 @@ export default function Navigation() {
4443
isIdle,
4544
isExecuting,
4645
isTransitioning,
47-
isPending,
4846
hasSucceeded,
4947
hasErrored,
5048
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"is-ci": "^4.1.0",
3939
"prettier": "^3.4.2",
4040
"prettier-plugin-tailwindcss": "^0.6.11",
41-
"turbo": "^2.5.0",
41+
"turbo": "^2.5.2",
4242
"typescript": "catalog:"
4343
},
4444
"packageManager": "[email protected]"

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ import type { InferInputOrDefault, StandardSchemaV1 } from "./standard-schema";
88
export const getActionStatus = <ServerError, S extends StandardSchemaV1 | undefined, CVE, Data>({
99
isIdle,
1010
isExecuting,
11+
isTransitioning,
1112
result,
1213
hasNavigated,
1314
hasThrownError,
1415
}: {
1516
isIdle: boolean;
1617
isExecuting: boolean;
18+
isTransitioning: boolean;
1719
hasNavigated: boolean;
1820
hasThrownError: boolean;
1921
result: SafeActionResult<ServerError, S, CVE, Data>;
2022
}): HookActionStatus => {
2123
if (isIdle) {
2224
return "idle";
23-
} else if (isExecuting) {
25+
} else if (isExecuting || isTransitioning) {
2426
return "executing";
2527
} else if (
2628
hasThrownError ||
@@ -46,7 +48,6 @@ export const getActionShorthandStatusObject = ({
4648
isIdle: status === "idle",
4749
isExecuting: status === "executing",
4850
isTransitioning,
49-
isPending: status === "executing" || isTransitioning,
5051
hasSucceeded: status === "hasSucceeded",
5152
hasErrored: status === "hasErrored",
5253
hasNavigated: status === "hasNavigated",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const useAction = <ServerError, S extends StandardSchemaV1 | undefined, C
3636

3737
const status = getActionStatus<ServerError, S, CVE, Data>({
3838
isExecuting,
39+
isTransitioning,
3940
result,
4041
isIdle,
4142
hasNavigated: navigationError !== null,
@@ -169,6 +170,7 @@ export const useOptimisticAction = <ServerError, S extends StandardSchemaV1 | un
169170

170171
const status = getActionStatus<ServerError, S, CVE, Data>({
171172
isExecuting,
173+
isTransitioning,
172174
result,
173175
isIdle,
174176
hasNavigated: navigationError !== null,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export type HookShorthandStatus = {
5151
isIdle: boolean;
5252
isExecuting: boolean;
5353
isTransitioning: boolean;
54-
isPending: boolean;
5554
hasSucceeded: boolean;
5655
hasErrored: boolean;
5756
hasNavigated: boolean;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const useStateAction = <ServerError, S extends StandardSchemaV1 | undefin
3131
const [clientInput, setClientInput] = React.useState<InferInputOrDefault<S, void>>();
3232
const status = getActionStatus<ServerError, S, CVE, Data>({
3333
isExecuting,
34+
isTransitioning,
3435
result: result ?? {},
3536
isIdle,
3637
// HACK: This is a workaround to avoid the status being "hasNavigated" when the action is executed.

pnpm-lock.yaml

Lines changed: 35 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ As you can see, here we display a greet message after the action is performed, i
7777
- `isIdle`: true if the action status is `idle`.
7878
- `isTransitioning`: true if the transition status from the `useTransition` hook used under the hood is `true`.
7979
- `isExecuting`: true if the action status is `executing`.
80-
- `isPending`: true if the action status is `executing` or `isTransitioning`.
8180
- `hasSucceeded`: true if the action status is `hasSucceeded`.
8281
- `hasErrored`: true if the action status is `hasErrored`.
8382
- `hasNavigated`: true if a `next/navigation` function was called inside the action.
8483

85-
For checking the action status, the recommended way is to use the `isPending` shorthand property. Using `isExecuting` or checking if `status` is `"executing"` could cause race conditions when using navigation functions, such as `redirect`.
84+
The `executing` status and `isExecuting` shorthand property include the transition state.
8685

8786
Explore a working example [here](<https://github.com/TheEdoRan/next-safe-action/tree/main/apps/playground/src/app/(examples)/hook>).

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,10 @@ export default function TodosBox({ todos }: Props) {
131131
- `isIdle`: true if the action status is `idle`.
132132
- `isTransitioning`: true if the transition status from the `useTransition` hook used under the hood is `true`.
133133
- `isExecuting`: true if the action status is `executing`.
134-
- `isPending`: true if the action status is `executing` or `isTransitioning`.
135134
- `hasSucceeded`: true if the action status is `hasSucceeded`.
136135
- `hasErrored`: true if the action status is `hasErrored`.
137136
- `hasNavigated`: true if a `next/navigation` function was called inside the action.
138137

139-
For checking the action status, the recommended way is to use the `isPending` shorthand property. Using `isExecuting` or checking if `status` is `"executing"` could cause race conditions when using navigation functions, such as `redirect`.
138+
The `executing` status and `isExecuting` shorthand property include the transition state.
140139

141140
Explore a working example [here](<https://github.com/TheEdoRan/next-safe-action/tree/main/apps/playground/src/app/(examples)/optimistic-hook>).

0 commit comments

Comments
 (0)