1

I am trying to change the style options for my react-select component (specifically trying to get rid of the border and change the font and text color) but am not sure how to fix the TypeScript errors. I have the following code:

import React, {useState} from 'react';
import Select, { ValueType } from 'react-select';

type OptionType = {
    value: string;
    label: string;
  };

const LanguageSelector = () => {
    const languageOptions: OptionType[] = [
        { value: 'English', label: 'EN' },
        { value: 'German', label: 'DE' },
        { value: 'French', label: 'FR' },
      ];
    const [selectedOption, setSelectedOption] = useState<ValueType<OptionType, false>>(languageOptions[0]);
    const handleChange = (option: ValueType<OptionType, false>) => setSelectedOption(option);
    const customStyles = value => ({
        control: (provided, state) => ({
          ...provided,
          alignItems: "baseline",
          backgroundColor: value ? "gray" : "white"
        })
      });

    return (
    <div>
        <Select
            value={selectedOption}
            onChange={handleChange}
            options={languageOptions}
            styles={customStyles}
        />
    </div>
    )
};

export default LanguageSelector;

The errors are that provided, state, and value all implicitly have an 'any' type. I'm assuming provided would have type CSSProperties, but am unsure about state. Does anyone know what these types should be?

2
  • what is the meaning of Control, provided and state here? Commented Mar 8, 2021 at 18:11
  • @sbuck89 Does my answer solves your problem? Commented Mar 17, 2021 at 8:51

1 Answer 1

1

First of all, I think you don't need the customStyles to be a function (as you are not using the value at all.)

Secondly, can add type to your customStyles with StylesConfig type, which expects OptionType, false as two generics.

const dropdownStyles: StylesConfig<OptionType, false> = {
  container: (provided) => ({
    ...provided,
    flexGrow: 1,
  }),
  control: (provided) => ({
    ...provided,
    background: '#fff',
    borderColor: '#9e9e9e',
    minHeight: '24px',
  }),
}

As a plus, you could even use OptionTypeBase from their types if you don't have extra properties in your options array's properties.

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.