Sagaryal
Sagaryal

Reputation: 415

Get specific selected value from multi react-select

Currently in multiple react-select (multi enabled), the onChange handler returns all the selected values as an array. But I would like to get only the selected item. How can I do that?

Also, is there a handler to know which item I have removed either by clicking x icon or by pressing backspace from multiple react-select options?

Upvotes: 1

Views: 300

Answers (1)

Parag Soni
Parag Soni

Reputation: 713

For newly selected value, I don't think it is possible. but you can perform subtraction in onChange handler.

For an example:

handleChange = (selectedOption) => {

  let arr1 = this.state.selectedOption; //already selected values and stored in this.state.selectedOption;
  let arr2 = selectedOption; // array with latest selected options.

  let difference = arr1
                 .filter(x => !arr2.includes(x))
                 .concat(arr2.filter(x => !arr1.includes(x)));

  console.log(difference);
}

ES6 is required.

Upvotes: 1

Related Questions