2

I'm using material-ui multiple select and I've created an example based on the documentation provided from Multiple Select.

My example here: codesandbox

In my example, I want to use 2 arrays for multiple select dropdowns, 1 for each. I tried to achieve this by updating the handleChange event from:

const handleChange = (event) => {
    setState(event.target.value);
  };

to:

const handleChange = event => {
    setState({ ...state, [event.target.name]: event.target.value });
  };

When I test this update, I click on any name from the drop-down and I get this error: state.indexOf is not a function

I want to be able to use the handleChange event to work for all of the multiple select drop-downs for my example. Any help or suggestions are much appreciated.

1
  • Please change the handleChange() :-> const handleChange = event => { console.log(event); setState(event.target.value); }; Commented Jun 1, 2020 at 18:36

1 Answer 1

3

In your codesandbox, you have used only one array as state which should be changed to two

  const [state, setState] = React.useState({ first: [], second: [] });

For each select we have to give the name

<Select
      labelId="demo-mutiple-checkbox-label"
      id="demo-mutiple-checkbox"
      multiple
      value={state.first}
      name="first"
      onChange={handleChange}
      input={<Input />}
      renderValue={selected => selected.join(", ")}
    >

this is the working csb link https://codesandbox.io/s/material-demo-s1g4h?file=/demo.js:1322-1618

Sign up to request clarification or add additional context in comments.

Comments

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.