我认为正在发生的是
{...register("username")}
,它被拾取为
...props
在Input组件的props中,在引擎盖下使用ref(这就是react钩子表单库识别输入的方式)。您应该能够通过将Input组件转换为使用如下的forwardRef来解决此问题:
export const Input = React.forwardRef<HTMLInputElement, Omit<InputProps, "ref">>(({ name, label, error, ...props }, ref) => (
<div className={styles.wrapper}>
{!!label && (
<label htmlFor={name} className={styles.label}>
{label}
</label>
)}
<input id={name} name={name} className={styles.input} ref={ref} {...props} />
{!!error && <p className={styles.error}>{error}</p>}
</div>
));
Input.displayName = "Input";
基本上,这可以让你把
ref
的属性
Input
组件,该组件被“转发”到
input
组成部分这允许库监视更改。