0
import React, { useState } from "react";
import MultiSelect from "react-multi-select-component";
const Example = () => {
  const options = [
    { label: "Grapes", value: "grapes" },
    { label: "Mango", value: "mango" },
    { label: "Strawberry", value: "strawberry", disabled: true },
    { label: "Watermelon", value: "watermelon" },
    { label: "Pear", value: "pear" },
  ];
const [selected, setSelected] = useState([]);
return (
  <div>
   <h1>Select Fruits</h1>
   <pre>{JSON.stringify(selected)}</pre>
   <MultiSelect
    options={options}
    value={selected}
    onChange={setSelected}
    labelledBy="Select"
   />
  </div>
 );
};

export default Example;

This code is working fine. But i'm not getting how to checked items dynamically. how should i passed checked value for 2 and 3 options if i want. Thank in advance

6
  • Are you having an issue selecting multiple items or are you trying to set the starting value? Commented Jun 17, 2021 at 16:30
  • i'm able to select multiple options. after setting value={[...]} i'm not able to select any other option or not able to de-select which are already checked Commented Jun 17, 2021 at 16:40
  • is the "Custom Value Renderer" can be used for this purpose npmjs.com/package/react-multi-select-component Commented Jun 17, 2021 at 16:43
  • I just copied your original code you posted and it works fine. I am not sure what you are trying to accomplish? Commented Jun 17, 2021 at 16:53
  • 1
    @Rao I think I understand what you want now --- see my answer below Commented Jun 17, 2021 at 17:04

1 Answer 1

2

If you want the list to initialize with pre-selected items, you need to pass the selected prop correctly. Currently you pass an empty array. Try this:


import React, { useState } from "react";
import MultiSelect from "react-multi-select-component";
const App = () => {
    const options = [
          { label: "Grapes", value: "grapes" },
          { label: "Mango", value: "mango" },
          { label: "Strawberry", value: "strawberry", disabled: true},
          { label: "Watermelon", value: "watermelon" },
          { label: "Pear", value: "pear" },
        ];
  // preset some selected values for first render
  const [selected, setSelected] = useState(options.slice(0,2)); 
  
  return (
      <div>
           <h1>Select Fruits</h1>
           <pre>{JSON.stringify(selected)}</pre>
           <MultiSelect
                 options={options}
                 value={selected}
                 onChange={setSelected}
                 labelledBy="Select"
                />
          </div>
     );
};

export default App;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Scott, i'm able to checked values using this solution. but not able to check or uncheck options. any suggestion
Thank you @scott, this working exactly how i need.

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.