Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get react-select selected option when using multi dropdown

I have two working react-select drop downs on my page, one that allows the user to select A or B, and one that allows them to choose multiple items from "blue, yellow , red".

When they have chosen these items, I want to use them. For now I just want to check the values that have been selected, so I'm just printing them to screen. For the single selection drop down I have used the example code from the github successfully. This is as follows:

import React from 'react';
import Select from 'react-select';

const options = [
  { value: 'a', label: 'a' },
  { value: 'b', label: 'b' },
];

class App extends React.Component {
  state = {
    selectedOption: null,
  }
  handleChange = (selectedOption) => {
    this.setState({ selectedOption });
    document.write(`Option selected:`, selectedOption.value); //this prints the selected option
  }
  render() {
    const { selectedOption } = this.state;

    return (
      <Select
        value={selectedOption}
        onChange={this.handleChange}
        options={options}
        //isMulti //added when the user can pick more than one
      />
    );
  }
}

My question is how do I successfully do this for the multi option? The user can select as many as they wish, but an 'undefined' error is thrown when it prints the option that has been selected. I think this is because the option is stored in an array but I'm not sure.

Thanks all.

like image 846
Uciebila Avatar asked Nov 07 '18 14:11

Uciebila


People also ask

What is multi select dropdown in react?

REACT MULTISELECT DROPDOWN A React component which provides multi select functionality with various features like selection limit, CSS customization, checkbox, search option, disable preselected values, flat array, keyboard navigation for accessibility and grouping features.

How to show multiple values on if selected in react select component?

The <Select> component provided by the react-select package, having three properties value, onChange event handler and options to take a collection of data. The isMulti property is set to true to enable multi selection of values in the React Select component. Here we will use ternary operator to show values on if selected.

How to create a react select dropdown using bootstrap?

We declared the render () method and passed the HTML code inside of it such as container, row, and column from Bootstrap library to create the basic layout in our React app. Then, we declared the React select dropdown with the options= {...} object and inside the options tag we passed the Countries array.

What is react-select?

React Dropdown Select Tutorial with React-select. React-Select is profoundly easy to use dropdown library exclusively built for React. The react-select library offers powerful multi-select, autocomplete, and AJAX support without any hassle. React-select’s main power lies in its dynamic functionalities such as search, filter, async loading, ...


1 Answers

You need to change the handleChange to deal with the isMulti. Here's an example:

import React, { Component } from 'react';
import { render } from 'react-dom';
import Select from 'react-select';

const options = [
  { value: 'a', label: 'a' },
  { value: 'b', label: 'b' },
];

class App extends React.Component {
  state = {
    selectedOptions: [],
  }

  handleChange = (selectedOptions) => {
    this.setState({ selectedOptions });
  }

  render() {
    const { selectedOptions } = this.state;

    return (
      <React.Fragment>
        <Select
          isMulti
          value={selectedOption}
          onChange={this.handleChange}
          options={options}
        />
      {selectedOptions.map(o => <p>{o.value}</p>)}
      </React.Fragment>
    );
  }
}

render(<App />, document.getElementById('root'));

Here's a working example: https://stackblitz.com/edit/react-czf4ib

like image 121
Colin Ricardo Avatar answered Sep 21 '22 20:09

Colin Ricardo