Closed martinlehoux closed 3 years ago
Hi Martin. To add a ref you need to pass a custom prop named domRef (instead of ref)
You can check it here https://github.com/couds/react-bulma-components#adding-ref-to-a-component
Hi @martinlehoux
You could also use it like this
const { register, handleSubmit } = useForm()
const { ref: nameRef, ...nameProps} = register('name')
const { ref: companyIdRef, ...companyIdProps} = register('companyId')
<Input {...nameProps)} domRef={nameRef} />
<Select {...companyIdProps} domRef={companyIdRef}>
<option value=''>Select company</option>
{companies?.map(c => (
<option key={c.id} value={c.id}>{c.name}</option>
))}
</Select>
Thanks for the tip @couds !
A simpler solution that I ended up using is a utility function.
export function bulmaize<T extends { ref: any }>(registration: T): Omit<T, "ref"> | { domRef: any } {
const {
ref: domRef,
...others
} = registration
return {
...others,
domRef,
}
}
And register using {...bulmaize(form.register("email", { required: false }))}
.
Describe the bug
This is a warning from React. I can't exactly say what it may cause.
I am using
react-hook-form@7.7.1
, and I have this warning when I register my Input.My Select does not work: when I select an option, it instantly switches back to the default one. It may or not be related to this issue.
To Reproduce
<Input {...register('name')} /> <Select {...register('companyId')}>
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?