Skip to content

Commit

Permalink
fix(hooks): use React namespace import
Browse files Browse the repository at this point in the history
Vitest fails to run tests if React hook functions are imported via named imports, so switching to a
namespace import fixes the issue.

re #54
  • Loading branch information
TheEdoRan committed Jan 23, 2024
1 parent 1899932 commit fbaddc8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
38 changes: 19 additions & 19 deletions packages/next-safe-action/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { InferIn, Schema } from "@decs/typeschema";
import { isNotFoundError } from "next/dist/client/components/not-found.js";
import { isRedirectError } from "next/dist/client/components/redirect.js";
import { useCallback, useEffect, useOptimistic, useRef, useState, useTransition } from "react";
import * as React from "react";
import {} from "react/experimental";
import type { SafeAction } from ".";
import type { MaybePromise } from "./utils";
Expand Down Expand Up @@ -76,13 +76,13 @@ const useActionCallbacks = <const S extends Schema, const Data>(
reset: () => void,
cb?: HookCallbacks<S, Data>
) => {
const onExecuteRef = useRef(cb?.onExecute);
const onSuccessRef = useRef(cb?.onSuccess);
const onErrorRef = useRef(cb?.onError);
const onSettledRef = useRef(cb?.onSettled);
const onExecuteRef = React.useRef(cb?.onExecute);
const onSuccessRef = React.useRef(cb?.onSuccess);
const onErrorRef = React.useRef(cb?.onError);
const onSettledRef = React.useRef(cb?.onSettled);

// Execute the callback when the action status changes.
useEffect(() => {
React.useEffect(() => {
const onExecute = onExecuteRef.current;
const onSuccess = onSuccessRef.current;
const onError = onErrorRef.current;
Expand Down Expand Up @@ -121,14 +121,14 @@ export const useAction = <const S extends Schema, const Data>(
safeAction: SafeAction<S, Data>,
callbacks?: HookCallbacks<S, Data>
) => {
const [, startTransition] = useTransition();
const [result, setResult] = useState<HookResult<S, Data>>(DEFAULT_RESULT);
const [input, setInput] = useState<InferIn<S>>();
const [isExecuting, setIsExecuting] = useState(false);
const [, startTransition] = React.useTransition();
const [result, setResult] = React.useState<HookResult<S, Data>>(DEFAULT_RESULT);
const [input, setInput] = React.useState<InferIn<S>>();
const [isExecuting, setIsExecuting] = React.useState(false);

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

const execute = useCallback(
const execute = React.useCallback(
(input: InferIn<S>) => {
setInput(input);
setIsExecuting(true);
Expand All @@ -151,7 +151,7 @@ export const useAction = <const S extends Schema, const Data>(
[safeAction]
);

const reset = useCallback(() => {
const reset = React.useCallback(() => {
setResult(DEFAULT_RESULT);
}, []);

Expand Down Expand Up @@ -182,19 +182,19 @@ export const useOptimisticAction = <const S extends Schema, const Data>(
reducer: (state: Data, input: InferIn<S>) => Data,
callbacks?: HookCallbacks<S, Data>
) => {
const [, startTransition] = useTransition();
const [result, setResult] = useState<HookResult<S, Data>>(DEFAULT_RESULT);
const [input, setInput] = useState<InferIn<S>>();
const [isExecuting, setIsExecuting] = useState(false);
const [, startTransition] = React.useTransition();
const [result, setResult] = React.useState<HookResult<S, Data>>(DEFAULT_RESULT);
const [input, setInput] = React.useState<InferIn<S>>();
const [isExecuting, setIsExecuting] = React.useState(false);

const [optimisticData, setOptimisticState] = useOptimistic<Data, InferIn<S>>(
const [optimisticData, setOptimisticState] = React.useOptimistic<Data, InferIn<S>>(
initialOptimisticData,
reducer
);

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

const execute = useCallback(
const execute = React.useCallback(
(input: InferIn<S>) => {
setInput(input);
setIsExecuting(true);
Expand All @@ -218,7 +218,7 @@ export const useOptimisticAction = <const S extends Schema, const Data>(
[setOptimisticState, safeAction]
);

const reset = useCallback(() => {
const reset = React.useCallback(() => {
setResult(DEFAULT_RESULT);
}, []);

Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"esModuleInterop": true,
"resolveJsonModule": true,
"incremental": false,
"noEmit": true
"noEmit": true,
},
"exclude": ["node_modules"],
"include": ["tsup.config.ts", "./src/**/*.ts"]
"include": ["tsup.config.ts", "./src/**/*.ts"],
}

0 comments on commit fbaddc8

Please sign in to comment.