Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize only one option from react-select?

I'm working with react-select and I want to customize only one option from the drop-down. Is there such an opportunity? I would like to do something like:

const CustomOption = ({ innerRef, innerProps, data }) => data.custom
    ? (<div ref={innerRef} {...innerProps} >I'm a custom link</div>)
    : defaultOne //<--- here I would like to keep default option

    <ReactSelect
        components={{ Option: CustomOption }}
        options={[
            { value: 'chocolate', label: 'Chocolate' },
            { value: 'strawberry', label: 'Strawberry' },
            { value: 'vanilla', label: 'Vanilla' },
            { custom: true },
    ]}
/>

Any thoughts how to achive that?

like image 717
Dominik Krzywiecki Avatar asked Dec 18 '25 17:12

Dominik Krzywiecki


1 Answers

Your feeling is good, you can achieve your goal with the following way:

const CustomOption = props => {
  const { data, innerRef, innerProps } = props;
  return data.custom ? (
    <div ref={innerRef} {...innerProps}>
      I'm a custom link
    </div>
  ) : (
    <components.Option {...props} />
  );
};

const options = [
  { value: "chocolate", label: "Chocolate" },
  { value: "strawberry", label: "Strawberry" },
  { value: "vanilla", label: "Vanilla" },
  { custom: true }
];

function App() {
  return <Select components={{ Option: CustomOption }} options={options} />;
}

The important thing to notice is to pass the entire props property to the components.Option to have the default behaviour.

Here a live example.

like image 171
Laura Avatar answered Dec 20 '25 14:12

Laura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!