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

274
Visualizações
How to return the old values on click of discard (close) icon while editing a material-ui table record?

On top of how to edit a particular row in material-ui table?, whenever I am clicking on the X icon while editing a row, the changes still reflecting in the table. Is there any way to overcome it? The edited record should only save on check (_/) icon. I am fully blank on this now.

Playground: https://codesandbox.io/s/material-ui-editable-tables-wsp0c?file=/package.json:0-677

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is in your onToggleEditMode method. You are preserving the previous value fine during the onChange handler, and you are replacing the temporary value with the previous one during the onRevert handler properly. However, in that onRevert handler, you update the rows state with setRows and then call the onToggleEditMode function directly after, which still references the stale rows state object.

const onToggleEditMode = id => {
  setRows(state => {
    return rows.map(row => {
      if (row.id === id) {
        return { ...row, isEditMode: !row.isEditMode };
      }
      return row;
    });
  });
};

You need to use that state parameter on the second line so that you do not override the new value you just saved and create a race condition. I updated the method to the following, (I also cleaned up the row map method) and it worked in the sandbox:

const onToggleEditMode = (id) => {
  setRows((state) =>
    state.map((row) => ({
      ...row,
      isEditMode: row.id === id ? !row.isEditMode : row.isEditMode
    }))
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The example you provided shows that onChange() overrides the rows:

setRows(newRows);

My suggestion would be to create a draft copy of the edited row (since only one can be edited at the same time). Then overwrite the actual data with the draft if you click "approve" otherwise do nothing. The behavior you describe is really just the custom logic in the code, there is nothing MUI-specific about it. You will best learn how to do it if you do it on your own. I can check your example though.

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