How can i watch onChange on the form element? #9538
Unanswered
livevsonline
asked this question in
Q&A
Replies: 2 comments 1 reply
-
you can just attach |
Beta Was this translation helpful? Give feedback.
1 reply
-
You can subscribe to formState changes in a useEffect hook and run callback you need. const form = useForm<FormSchema>({
defaultValues: normalizeInput(defaultValues),
});
const onSubmit = form.handleSubmit((values: FormSchema) => {
return onConfirm?.(normalizeOutput(values));
});
useEffect(() => {
return form.subscribe({
formState: {
values: true,
},
callback: ({values}) => {
onChange?.(normalizeOutput(values));
},
});
}, [form.subscribe]); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi i am in a situation where i have to implement automatic search on form change, the onChange event has to call a custom function when an input is being changed, not necessarily re-rendering and calling the validation on each change, for instance i could have a setTimeout inside the custom function and clear that setTimeout on each change event until the user is finished inputting for x amount of time.
At first i tried to use a useEffect to capture touched fields like this:
This does trigger when a field is touched, however it does also trigger when the user stop focusing the input field as well as when the form is attempted to be submitted...
Then i tried to register the form element itself and i guess, bind an event, like so:
If i try to attach a onChange event directly on the form it is not fired upon change as i am expecting it to
What am i doing wrong, and how can i attach an onChange event on the form? :)
Beta Was this translation helpful? Give feedback.
All reactions