How to get selected by only value for multi select in react-select

Today we’ll show you how to get selected by only value for multi select in react-select. Previously we have written an article to get selected by value for a single selection but as per our reader’s request, we are going to write this article for multiple select options in react dropdown.

We will take an example where we use the isMulti attribute to enable the multiple selection options and isClearable attribute to clear value in react-select.

Steps to get selected by value for multi-select

  1. Implement react-select dropdown
  2. Enable multi select and clearable features
  3. Write logic to get selected by value for multi-select
  4. Output

1. Implement react-select dropdown

To create an example, first we’ll set up a simple react application that contains only a single react-select dropdown and label to display the selected values.

Refer an article to Implement dropdown in ReactJS.

2. Enable multi select and clearable features

Let’s enable the multi select and clearable features for react-select dropdown. For that we have to add isMulti & isClearable attributes in the dropdown component.

3. Write logic to get selected by value for multi-select

Use state to handle the selected value and here we are working with multi-select dropdown so the initial state value should be a blank array.

Create a handler for react-select onChange event where we’ll use only selected values in the form of the array and set it in the state variable.

Finally, we need to change the logic of the value attribute of the react-select dropdown and return the list of objects based on the selected values.

4. Output

Your file should look like this after combining all code together.

App.js

Output - How to get selected by only value for multi select in react-select - Clue Mediator
Output – How to get selected by only value for multi select in react-select – Clue Mediator

That’s it for today.
Thank you for reading. Happy Coding..!!

Demo & Source Code

Github Repository StackBlitz Project
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

10 Responses

  1. ayushi jaiswal says:

    How can send the selected values as an axios post request?

    • Clue Mediator says:

      Hi Ayushi,
      You can store the comma-separated values in the database as below.

      axios.post('/user', {
      lang: '2,3,5'
      }).then(function (response) {
      console.log(response);
      }).catch(function (error) {
      console.log(error);
      });

  2. Sheldon says:

    How can we use the selected values to display a related message. For e.g , i have values as cough, pain and dizzy. how do i make it so that when these values are selected , message is displayed saying , you have common cold?

    • Clue Mediator says:

      You have to do some checks in the onChange method of the dropdown list and depending on the condition you can set the state variable to display text.

      If you have Stackblitz source code then share it with us so we can provide further guidance.

        • Clue Mediator says:

          Set the category in the list and use it for display text.

          const data = [
          { label: "Fever", value: 1, category: "cat 1" },
          { label: "Head-ache", value: 2, category: "cat 2" },
          { label: "Runny-nose", value: 3, category: "cat 1" },
          { label: "Ear-pain", value: 4, category: "cat 1" },
          { label: "Body-pain", value: 5, category: "cat 2" },
          { label: "Cough", value: 6, category: "cat 1" }
          ];

          const Diagnose = e => {
          getValue([...new Set(e.map(x => x.category))]);
          };

  3. ravi says:

    thanks bro

  4. Iftikhar Ali says:

    Hmmm, Very easy and great article.. Thanks a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *