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

201
Visualizações
Value inside dropdown doesn't seem to change with onChange handler - MUI Select using React

Trying to update dropdown values using MUI's Select component but I can't get to update using onChange handler, the value remains same always even though I select a new item in the dropdown.

I created a working example using CodeSanbox. Could anyone please help?

Excerpt from my code

export default function Cars() {
  const rows = [
    {
      make: "BMW",
      model: "X3",
      type: "Suv"
    },
    {
      make: "VW",
      model: "Jetta",
      type: "Sedan"
    }
  ];

  const handleChange = (e, row, index) => {
    console.log("dropdown value -> ", e.target.value);
    console.log("row -> ", row);
    row.type = e.target.value;
  };

  return (
    <div>
      <TableContainer>
        <Table>
          <TableHead>
            <TableRow>
              <TableCell>Make</TableCell>
              <TableCell>Model</TableCell>
              <TableCell>Type</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {rows.map((row, index) => (
              <TableRow key={row.make}>
                <TableCell component="th" scope="row">
                  {row.make}
                </TableCell>
                <TableCell component="th" scope="row">
                  {row.model}
                </TableCell>
                <TableCell component="th" scope="row">
                  <FormControl>
                    <InputLabel>Type</InputLabel>
                    <Select
                      value={row.type}
                      label="Type"
                      onChange={(e) => handleChange(e, row, index)}
                    >
                      <MenuItem value="Sedan">Sedan</MenuItem>
                      <MenuItem value="Suv">Suv</MenuItem>
                    </Select>
                  </FormControl>
                </TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </TableContainer>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should use useState to keep the current state of your rows data and update the array in your handleChange function like this:

const [rows, setRows] = useState([
    {
      make: "BMW",
      model: "X3",
      type: "Suv"
    },
    {
      make: "VW",
      model: "Jetta",
      type: "Sedan"
    }
]);

const handleChange = (e, row, index) => {
    console.log("dropdown value -> ", e.target.value);
    console.log("row -> ", row);
    const copyRows = [...rows];
    copyRows[index].type = e.target.value;
    setRows(copyRows);
};

You can take a look at this forked sandbox for a live working example.

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