12

I am trying to create custom components for DropdownIndicator and use on react-select with Typescript, but I am having some issues with the type of the component because I am new to typescript.

How can I use types defined on @types/react-select on my component?

I've installed @types/react-select and there is already a type for DropdownIndicator, but I've found no way to reference it.

This is my index.tsx:

import React from 'react';
import Select from 'react-select';

import DropdownIndicator from './dropdown-indicator';

const components = {
  DropdownIndicator,
};

const SelectFilter: React.FC = () => {
  return <Select components={components} />;
};

export default React.memo(SelectFilter);

This is my dropdown-indicator.tsx:

import React from 'react';

import DropdownIcon from './dropdown-icon';

const DropdownIndicator: React.FC = props => {
  const {
    selectProps: { isMenuOpen },
  } = props;

  return <DropdownIcon isUp={isMenuOpen} />;
};

export default React.memo(DropdownIndicator);

Since I didn't defined any prop types, the error is:

/Users/felipepinheiro/Workspace/test/src/components/select-filter/dropdown-indicator.tsx
TypeScript error in /Users/felipepinheiro/Workspace/test/src/components/select-filter/dropdown-indicator.tsx(7,5):
Property 'selectProps' does not exist on type '{ children?: ReactNode; }'.  TS2339

     5 | const DropdownIndicator: React.FC = props => {
     6 |   const {
  >  7 |     selectProps: { isMenuOpen },
       |     ^
     8 |   } = props;
     9 | 
    10 |   return <DropdownIcon isUp={isMenuOpen} />;
2
  • use props.selectProps.menuIsOpen not isMenuOpen ;) and do not destruct them. Commented Sep 24, 2019 at 9:58
  • You could do something like this where you pass the component directly into the function and import the IndicatorProps type ts import {IndicatorProps } from 'react-select'; const SelectFilter: React.FC = () => { return <Select components={{ Indicator: (indicatorProps: IndicatorProps<any>) => ( <components.DropdownIndicator {...indicatorProps} /> ), }} />; }; Commented Nov 27, 2019 at 23:57

1 Answer 1

8

I left a comment but putting this in an answer since it'll be more legible with proper formatting

You can directly import the IndicatorProps type from react-select package and then pass your props to the component directly from within your select component

import { IndicatorProps, OptionType } from 'react-select';


const SelectFilter: React.FC = () => {
  return <Select components={{
    Indicator: (indicatorProps: IndicatorProps<OptionType>) => (
      <components.DropdownIndicator {...indicatorProps} />
    ),
  }} />;
};
Sign up to request clarification or add additional context in comments.

4 Comments

What is the right way to avoid any inside Record<string, unknown>?
I think if you import the OptionType type and use that instead of <any> it should work correctly
It works. But there is a problem with @typescript-eslint/no-explicit-any ESLint rule, and I'm asking to figure out how to overcome it correctly.
I'm familiar with the rule, so I updated my above example to include my proposed answer that uses <OptionType> instead of <any>. Where are you trying to use Record<string, unknown>? If you show me an example I might be able to better help

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.