Skip to content

Commit 16e5693

Browse files
committedJun 12, 2024··
fix(hooks): useStateAction execution flow
1 parent 68d8ed2 commit 16e5693

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed
 

‎packages/next-safe-action/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@eslint/js": "^9.2.0",
6666
"@types/node": "^20.12.10",
6767
"@types/react": "^18.3.1",
68+
"@types/react-dom": "18.3.0",
6869
"@typeschema/core": "^0.13.2",
6970
"eslint": "^8.57.0",
7071
"eslint-config-prettier": "^9.1.0",
@@ -73,6 +74,7 @@
7374
"next": "14.3.0-canary.42",
7475
"prettier": "^3.2.5",
7576
"react": "18.3.1",
77+
"react-dom": "18.3.1",
7678
"semantic-release": "^23.0.8",
7779
"tsup": "^8.0.2",
7880
"tsx": "^4.11.2",
@@ -82,6 +84,7 @@
8284
"peerDependencies": {
8385
"next": ">= 14.0.0",
8486
"react": ">= 18.2.0",
87+
"react-dom": ">= 18.2.0",
8588
"zod": ">= 3.0.0"
8689
},
8790
"peerDependenciesMeta": {

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { InferIn, Schema } from "@typeschema/main";
44
import { isNotFoundError } from "next/dist/client/components/not-found.js";
55
import { isRedirectError } from "next/dist/client/components/redirect.js";
66
import * as React from "react";
7+
import * as ReactDOM from "react-dom";
78
import {} from "react/experimental";
89
import type {} from "zod";
910
import type {
@@ -374,15 +375,21 @@ export const useStateAction = <
374375
);
375376
const [isIdle, setIsIdle] = React.useState(true);
376377
const [clientInput, setClientInput] = React.useState<S extends Schema ? InferIn<S> : void>();
377-
const status = getActionStatus<ServerError, S, BAS, CVE, CBAVE, Data>({ isExecuting, result, isIdle });
378+
const status = getActionStatus<ServerError, S, BAS, CVE, CBAVE, Data>({
379+
isExecuting,
380+
result: result ?? EMPTY_HOOK_RESULT,
381+
isIdle,
382+
});
378383

379384
const execute = React.useCallback(
380385
(input: S extends Schema ? InferIn<S> : void) => {
381-
setTimeout(() => {
386+
dispatcher(input as S extends Schema ? InferIn<S> : undefined);
387+
388+
// eslint-disable-next-line
389+
ReactDOM.flushSync(() => {
382390
setIsIdle(false);
383391
setClientInput(input);
384-
dispatcher(input as S extends Schema ? InferIn<S> : undefined);
385-
}, 0);
392+
});
386393
},
387394
[dispatcher]
388395
);

‎pnpm-lock.yaml

+24-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.