Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda