Skip to content

Commit

Permalink
refactor(hooks): don't use ref for safeAction input
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Nov 23, 2023
1 parent 4b7d071 commit ba3a00d
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions packages/next-safe-action/src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const useActionCallbacks = <const Schema extends z.ZodTypeAny, const Data>(
const onErrorRef = useRef(cb?.onError);
const onSettledRef = useRef(cb?.onSettled);

// Execute the callback on success or error, if provided.
// Execute the callback when the action status changes.
useEffect(() => {
const onExecute = onExecuteRef.current;
const onSuccess = onSuccessRef.current;
Expand Down Expand Up @@ -76,7 +76,7 @@ const useActionCallbacks = <const Schema extends z.ZodTypeAny, const Data>(
/**
* Use the action from a Client Component via hook.
* @param safeAction The typesafe action.
* @param callbacks Optional callbacks executed when the action succeeds or fails.
* @param callbacks Optional callbacks executed based on the action status.
*
* {@link https://next-safe-action.dev/docs/usage-from-client/hooks/useaction See an example}
*/
Expand All @@ -85,33 +85,34 @@ export const useAction = <const Schema extends z.ZodTypeAny, const Data>(
callbacks?: HookCallbacks<Schema, Data>
) => {
const [, startTransition] = useTransition();
const executor = useRef(safeAction);
const [result, setResult] = useState<HookResult<Schema, Data>>(DEFAULT_RESULT);
const [input, setInput] = useState<z.input<Schema>>();
const [isExecuting, setIsExecuting] = useState(false);

const status = getActionStatus<Schema, Data>(isExecuting, result);

const execute = useCallback((input: z.input<Schema>) => {
setInput(input);
setIsExecuting(true);

return startTransition(() => {
return executor
.current(input)
.then((res) => setResult(res ?? DEFAULT_RESULT))
.catch((e) => {
if (isNextRedirectError(e) || isNextNotFoundError(e)) {
throw e;
}

setResult({ fetchError: isError(e) ? e.message : "Something went wrong" });
})
.finally(() => {
setIsExecuting(false);
});
});
}, []);
const execute = useCallback(
(input: z.input<Schema>) => {
setInput(input);
setIsExecuting(true);

return startTransition(() => {
return safeAction(input)
.then((res) => setResult(res ?? DEFAULT_RESULT))
.catch((e) => {
if (isNextRedirectError(e) || isNextNotFoundError(e)) {
throw e;
}

setResult({ fetchError: isError(e) ? e.message : "Something went wrong" });
})
.finally(() => {
setIsExecuting(false);
});
});
},
[safeAction]
);

const reset = useCallback(() => {
setResult(DEFAULT_RESULT);
Expand All @@ -133,7 +134,8 @@ export const useAction = <const Schema extends z.ZodTypeAny, const Data>(
* **NOTE: This hook uses an experimental React feature.**
* @param safeAction The typesafe action.
* @param initialOptimisticData Initial optimistic data.
* @param callbacks Optional callbacks executed when the action succeeds or fails.
* @param reducer Optimistic state reducer.
* @param callbacks Optional callbacks executed based on the action status.
*
* {@link https://next-safe-action.dev/docs/usage-from-client/hooks/useoptimisticaction See an example}
*/
Expand All @@ -146,16 +148,13 @@ export const useOptimisticAction = <const Schema extends z.ZodTypeAny, const Dat
const [, startTransition] = useTransition();
const [result, setResult] = useState<HookResult<Schema, Data>>(DEFAULT_RESULT);
const [input, setInput] = useState<z.input<Schema>>();
const [isExecuting, setIsExecuting] = useState(false);

const [optimisticData, setOptimisticState] = useOptimistic<Data, z.input<Schema>>(
initialOptimisticData,
reducer
);

const [isExecuting, setIsExecuting] = useState(false);

const executor = useRef(safeAction);

const status = getActionStatus<Schema, Data>(isExecuting, result);

const execute = useCallback(
Expand All @@ -165,8 +164,7 @@ export const useOptimisticAction = <const Schema extends z.ZodTypeAny, const Dat

return startTransition(() => {
setOptimisticState(input);
return executor
.current(input)
return safeAction(input)
.then((res) => setResult(res ?? DEFAULT_RESULT))
.catch((e) => {
if (isNextRedirectError(e) || isNextNotFoundError(e)) {
Expand All @@ -180,7 +178,7 @@ export const useOptimisticAction = <const Schema extends z.ZodTypeAny, const Dat
});
});
},
[setOptimisticState]
[setOptimisticState, safeAction]
);

const reset = useCallback(() => {
Expand Down

1 comment on commit ba3a00d

@vercel
Copy link

@vercel vercel bot commented on ba3a00d Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.