简体   繁体   中英

Setting multiple selected values using React-select

I am having a dropdown where a user can add multiple dropdowns and select a value in it. While making a GET request, i wanted to keep the selected value in the dropdown, but not sure how to do it. I am able to make the selected value in a single dropdown, but finding it difficult to keep the selected value in multiple dropdowns. I will be getting the value in an array like this

values = ["English","Ukraine","Japnese","Korean"];

Then in Select Dropdown for setting the single value i have written it like this:

options = [
{value: "english", label: "English"},
{value: "ukraine", label: "Ukraine"},
{value: "japnese", label: "Japnese"},
{value: "korean", label: "Korean"},
{value: "french", label: "French"}
];
<Select 
  className="profile-module-select-container"
  classNamePrefix="profile-module-select"                        
  options={options}
  onChange={selected => {
   this.handleDropdownSelect(selected, formKey);
  }}
  onMenuOpen={(e, i, o) => {
    this.setState({
      selectMenuOpen: true
    });
  }}
  onMenuClose={() => {
     this.setState({
       selectMenuOpen: false
     });
  }}
  name={name}
  value={options.filter((items) => { return items.value === values })}
  />

values is an array and you would have to loop it over as well and filter it against Options . Considering the current implementation, it would be a nested loop, yes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM