1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Error: src/components/Home.tsx(25,42): error TS2345: Argument of type '(e?: BaseSyntheticEvent<object, any, any> | undefined) => Promise<void>' is not assignable to parameter of type 'SubmitHandler<FieldValues>'.

1
Posted at

はじめに

Githubでnpm run buildしたさいに発生したエラーです。
useFormで陥りやすいミスをしていました。

問題

ローカル上では問題なかったのですが、Githubでnpm run bulidするさいにエラーが発生しました。

Home
    type FormValues = {
        ID: string;
    };

    export const Home: FC<FormValues> = memo(() => {
        const onClick = handleSubmit( async () => {
            const input_id = getValues("ID");
            const id = await supabase.from("users").select("user_id").eq("user_id", input_id).single();
            if (id.error) {
                alert("名刺が見つかりませんでした。IDを確認してください。");
                throw new Error(id.error.message);
            }
            if (id) {
                navigate(`/cards/${id.data.user_id}`);
            }
        });
	return (
	                <form onSubmit={handleSubmit(onClick)}>
	)

解決方法

handleSubmitを二重に使ってるのが原因でした。
onClickは「純粋なSubmitHandler」にする方向で修正しました。

handleSubmitは1回だけ使う
onSubmitは (data) => {} の形にする

あと、修正前はFC<FormValues>としていました。
これも一つの原因でした。
いつも型を記載するときの流れから、書いていました。

useForm<FormValues> =「フォームの中身の型」
FC<FormValues> =「コンポーネントのpropsの型」

Home
    type FormValues = {
        ID: string;
    };

    export const Home: FC = memo(() => {
        const { register, handleSubmit, formState: { errors } } = useForm<FormValues>();
        const onSubmit: SubmitHandler<FormValues> = async (data) => {
            const input_id = data.ID;
            const id = await supabase.from("users").select("user_id").eq("user_id", input_id).single();
            if (id.error) {
                alert("名刺が見つかりませんでした。IDを確認してください。");
                throw new Error(id.error.message);
            }
            if (id) {
                navigate(`/cards/${id.data.user_id}`);
            }
        };
	return (
	                <form onSubmit={handleSubmit(onSubmit)}>
	)

おわりに

どの型が使えるかは判断します。

参考文献

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?