Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
How can I get the value of the Autocomplete Select of material-ui?

How can I get the value of what was selected in the drop down? I have these codes below but it will show this in the console.log:

MUI: The getOptionLabel method of Autocomplete returned number (0) instead of a string for 0.

import React, { useState } from "react";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";

export default function ComboBox() {
  const [selected, setSelected] = useState(0);
  const handleChange = (e) => setSelected(e.target.value);
  console.log(selected);

  return (
    <Autocomplete
      disablePortal
      id="combo-box-demo"
      options={Items}
      sx={{ width: 300 }}
      value={selected}
      onChange={handleChange}
      renderInput={(params) => <TextField {...params} label="Items" />}
    />
  );
}


const Items = [
  { label: "Car" },
  { label: "Book" },
  { label: "Chair" },
  { label: "Pencil" }
];

Also, will this work in material-ui ^4.12.3? codesandbox link: https://codesandbox.io/s/combobox-material-demo-forked-g88fi

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you need to get the second parameter of handleChange. and it is value.

 const handleChange = (e,v) => setSelected(v);

like this:

import React, { useState } from "react";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";

export default function Edit() {
    const [selected, setSelected] = useState( { label: "" });
    const handleChange = (e,v) => setSelected(v);
    console.log(selected);

    return (
        <Autocomplete
            disablePortal
            id="combo-box-demo"
            options={Items}
            sx={{ width: 300 }}
            value={selected}
            onChange={handleChange}
            renderInput={(params) => <TextField {...params} label="Items" />}
        />
    );
}


const Items = [
    { label: "Car" },
    { label: "Book" },
    { label: "Chair" },
    { label: "Pencil" }
];

it works very well for me.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!