is it possible to Watch an input only on BLUR ? #3620
Replies: 5 comments 21 replies
-
|
nope at the moment with |
Beta Was this translation helpful? Give feedback.
-
|
So, no solution? you can't pass |
Beta Was this translation helpful? Give feedback.
-
|
I followed the suggestion on using onBlur instead of useWatch to handle logic while the input changes. However, when I use setValue to fille the form for the user first, the onBlur does not trigger. (useWatch would detect setValue changes) Is it possible to make setValue triggers onBlur on input? |
Beta Was this translation helpful? Give feedback.
-
|
To elaborate and clarify the comments above, this works... const { setValue } = useForm({
input1: "enter a value"
});
return (
<input
onBlur={ (e) => setValue( "input1", e.target.value ) }
/>
)The docs say it's best to register the input first before using |
Beta Was this translation helpful? Give feedback.
-
|
Found another solution that have the ability to trigger onBlur on the element and useWatch hook by using import { Controller, useFormContext } from "react-hook-form";
function TestComponent(props: { listenType: "onChange" | "onBlur" | "all" }) {
const hookForm = useFormContext();
return (
<>
<Controller
name={"name"}
control={hookForm?.control}
render={(renderProps) => {
return (
<>
<input
//# dont forget this spread
{...renderProps.field}
onChange={(event) => {
//# the logic lies here
if (
props.listenType == "onChange" ||
props.listenType == "all"
) {
renderProps.field.onChange(event.target.value);
}
}}
onBlur={(event) => {
//# the logic lies here
if (
props.listenType == "onBlur" ||
props.listenType == "all"
) {
renderProps.field.onChange(event.target.value);
}
}}
/>
</>
);
}}
/>
</>
);
}
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For performance, i would like to know if is it possible to watch an input only when onBlur ?
because for now when i watch an input the watch is triggered every time i add/remove a character !
THX
Beta Was this translation helpful? Give feedback.
All reactions