Typescript error with form and handleSubmit #8020
Replies: 9 comments 29 replies
-
Same issue for me coming from @typescript-eslint/eslint-plugin. Another related case would be not spreading the registered field properties: const field = register("field");
...
<input
ref={field.ref}
name={field.name}
onBlur={field.onBlur} // Same error here as onBlur returns Promise<void | boolean>
onChange={field.onChange} // Same error here as onChange returns Promise<void | boolean>
type="text"
/> |
Beta Was this translation helpful? Give feedback.
-
Hey! Please, take a look at this CSB and possibly tweak to match your use-case, where you get the error. |
Beta Was this translation helpful? Give feedback.
-
The documentation of @typescript-eslint suggests disable the rule only for that particular case. Maybe it's not a solution but it does the trick for me. |
Beta Was this translation helpful? Give feedback.
-
@SalahAdDin , @sergey-sivchenko : Since it's just a linting error from eslint rule, not typescript error, the last thing you can do is following its best practice to avoid potential issue, may be a promise handling wrapper like this:
Demo: https://codesandbox.io/s/serene-grass-vehq90?file=/src/App.tsx |
Beta Was this translation helpful? Give feedback.
-
I notice this issue too. react-hook-form/src/types/form.ts Line 597 in 8028e4d It seems that all the solutions so far are just ignoring this ESLint rule / have non-idiomatic code |
Beta Was this translation helpful? Give feedback.
-
I suspect this is a bug in react-hook-form. Before:
After:
The bug: |
Beta Was this translation helpful? Give feedback.
-
Are there any updates on the go-to implementation one should follow? |
Beta Was this translation helpful? Give feedback.
-
We need to continue the discussion around this. I just installed Changing my I suggest we either:
<form onSubmit={handleSubmitHelperFunction(handleSubmit(onSubmit)} /> Personally, I vote for option 1 because I think users shouldn't have to think about this issue. |
Beta Was this translation helpful? Give feedback.
-
FWIW I was using @paleite 's workaround for a while until noticing that the I started using this instead, which has been working better import { FormEventHandler, useCallback } from 'react';
import { FieldValues, useForm } from 'react-hook-form';
import { Form, useSubmit } from 'react-router-dom';
const submit = useSubmit(); // from react-router
const { handleSubmit } = useForm<SomeFormType>({
// ...
})
const onSubmit = useCallback<FormEventHandler>(
(event) =>
handleSubmit((values: FieldValues) => {
submit(values, {
// submit your values somehow, here using react-router's submit
});
})(event),
[handleSubmit, submit],
);
return (
<Form
method="post"
onSubmit={onSubmit}
>
// ....
</Form>
); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys,
I'm using this for handling my form states, together with Typescript, but now, I'm getting an error with type checking:
Promise-returning function provided to attribute where a void return was expected
.My code is this:
I know
handleSubmit
returns aPromise
, but I don't know how to solve this problem.Can anyone help me?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions