1

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>
  );
};
1
  • I see as property for the first time. Should you use render property as a render property pattern instead? Commented Mar 15, 2022 at 19:44

1 Answer 1

2

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>

  )
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.