I faced a similar issue recently when migrating to V7. If it can help anybody.
A parent component handling the form was passing down to a wrapper the register function, the wrapper passing it down again to an input that needed debouncing on change.
I called the register formLibraryRef
in case I wanted to use a different library later but overall I had to do something like:
const { onChange, ...rest } = formLibraryRef(inputName);
pass the onChange to the function that is itself passed to the native onChange event of the input:
const handleDebouncedChange: (event: React.ChangeEvent<HTMLInputElement>) => void = (
event: ChangeEvent<HTMLInputElement>,
) => {
onChange(event);
if (preCallback) {
preCallback();
}
debounceInput(event);
};
and then pass the rest to the input:
<input
aria-label={inputName}
name={inputName}
data-testid={dataTestId}
className={`form-control ...${classNames}`}
id={inputId}
onChange={handleDebouncedChange}
onFocus={onFocus}
placeholder={I18n.t(placeholder)}
{...rest}
/>
The register
section in the docs here: https://react-hook-form.com/migrate-v6-to-v7/ gives a bit more info on how to get the onChange and shows an example for Missing ref
.
onChange
prop in react-hook-form v7.0.0. – CopperyonChange={handleImageUpload)
should work. Can you share a CodeSandbox if you're running into any issues? – CopperyhandleImageUpload
function does get called when you upload a file. – CopperyonChange
is that in RHF v7.x calling...register('name')
spreads an object containing{onChange, onBlur, name, ref}
, so the custom handler will either be ignored or override RHF functionality. See @Bill's answer for the proper way to deal with this. – Pyrrhic