'React-select & React-hook-form Controller issue
I'm trying to implement register
from react-hook-forms
for react-select
but in order to do that I need to make use of Controller
from react-hook-forms
but when I use Controller
then my Select Component
bombs out with
TypeError: methods is null
On useController.ts
(Which is the package controller file itself)
I don't know why I am getting this and at this point, I would appreciate some assistance in resolving this issue. I've spent hours debugging and changing and looking at alternative solutions but nothing works
My code for my react-select component
:
import PropTypes from 'prop-types';
import Select from 'react-select';
import { Controller } from 'react-hook-form';
import { Container, SelectHeader, Validation } from './SelectSearch.style';
const SelectSearch = ({
name,
label,
header,
placeholder,
options,
isClearable,
isSearchable,
isDisabled,
isLoading,
isRequired,
error,
margin,
validationLabel,
control,
}) => {
return (
<Container margin={margin}>
<SelectHeader>{header}</SelectHeader>
<Controller
as={
<Select
label={label}
name={name}
placeholder={placeholder}
options={options}
isClearable={isClearable}
isSearchable={isSearchable}
isDisabled={isDisabled}
isLoading={isLoading}
isRequired={isRequired}
styles={style}
/>
}
control={control}
name={name}
rules={{ required: true }}
defaultValue={null}
/>
{!error && validationLabel && <Validation>{validationLabel}</Validation>}
<div>{error}</div>
</Container>
);
};
Solution 1:[1]
The error message could be better but this error happens when you are using useForm
and forgetting to use the context form provider
import React from "react"
import { useForm, FormProvider } from "react-hook-form"
const MyFormComponent = () => {
const methods = useForm()
return (
<FormProvider {...methods} > // pass all methods into the context
...
</FormProvider>
)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gabriel Brito |