2

I am using react-select and I want to clear the selected value on button click without adding it to the options property

I have tried

  1. Using state to manipulate the options property , but Apparently on calling clearValue() method, automatically pushes to the options array by default.

Problem

How do I tell react-select to just clear the selected values and not add it in the options array anywhere ?

import React, { useRef } from "react";
import Select from "react-select";

export default function App() {
  const selectInputRef = useRef();

  const onClear = () => {
    selectInputRef.current.select.clearValue();
  };

  return (
    <div className="App">
      <h1>Select Gender</h1>
      <Select
        isMulti
        ref={selectInputRef}
        options={[
          { value: "male", label: "Male" },
          { value: "female", label: "Female" }
        ]}
      />
      <button onClick={onClear}>Clear Value</button>
    </div>
  );
}

Here is my CodeSandbox Link

10
  • In other words: You want to remove the currently selected value from the options alltogether? Commented Feb 18, 2021 at 8:34
  • @trixn yes that is what i want .. ideally it should be a function call .. so for ex I can remove those options on save Commented Feb 18, 2021 at 8:38
  • 1
    In that case you need to manage the options in component state and remove it, when the button is clicked. Commented Feb 18, 2021 at 8:38
  • @trixn I don't want react-select to change it's default behaviour of re adding elements.. i Just want to do it on a particular time i.e onButtonClick Commented Feb 18, 2021 at 8:39
  • @trixn I have already tried it in prod version of the code.. it doesnt work .. react-select automatically re adds them .. i would appreciate it if you would help me :( Commented Feb 18, 2021 at 8:40

2 Answers 2

4

You need to manage the state of the selected values and the options yourself and manipulate it accordingly:

export default function App() {
  const [selected, setSelected] = useState([]);
  const [options, setOptions] = useState([
    { value: "male", label: "Male" },
    { value: "female", label: "Female" }
  ]);

  const handleClear = () => {
    setOptions((currentOptions) => currentOptions.filter((currentOption) => !selected.includes(currentOption)));
    setSelected([]);
  };

  return (
    <div className="App">
      <h1>Select Gender</h1>
      <Select isMulti value={selected} options={options} onChange={setSelected} />
      <button onClick={handleClear}>Clear Value</button>
    </div>
  );
}

Edit react-select-clear-input (forked)

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

Comments

4

Why not just add isClearable prop as:

<Select
  isMulti
  value={selected}
  options={options}
  onChange={setSelected}
  isClearable
/>

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.