Skip to content

Instantly share code, notes, and snippets.

@paambaati
Created April 12, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paambaati/ac72a90df09481b7f4424ae1d878dacf to your computer and use it in GitHub Desktop.
Save paambaati/ac72a90df09481b7f4424ae1d878dacf to your computer and use it in GitHub Desktop.
react-select + react-hook-form
import { forwardRef } from 'react';
import Select from 'react-select';
import { Controller } from 'react-hook-form';
import type { RefObject } from 'react';
import type { Props as ReactSelectProps } from 'react-select';
const Select = forwardRef(
(props: SelectProps, ref: RefObject<HTMLSelectElement>) => {
return (
<Controller
ref={ref}
as={<Select options={props.options} />}
onChange={([selected]) => {
console.log('selected = ', selected);
return selected;
}}
{...props}
/>
);
}
);
export type SelectProps = ReactSelectProps & {
controllerProps?: any;
};
export default Select;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment