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

154
Visualizações
Updating state adds new objects instead of updating existing objects

I am having a problem with trying to update my state. I am trying to update the items inside my existing objects whenever I drop the element however whenever I update it, it adds a new object to my array instead of updating them. What am I doing wrong?

const handleDragEnd = (event) => {
    const { active, over } = event;
    const activeContainer = event.active.data.current.sortable.containerId;
    const overContainer = event.over.data.current.sortable.containerId;

    if (active.id !== over.id) {
      const oldIndex = columns[activeContainer].items.findIndex(
        ({ id }) => id === active.id
      );
      const newIndex = columns[overContainer].items.findIndex(
        ({ id }) => id === over.id
      );
      const result = arrayMove(
        columns[overContainer].items,
        oldIndex,
        newIndex
      );

      console.log("oldindex", columns[activeContainer].items);
      console.log("newindex", result);
      if (activeContainer !== overContainer) {
      }
      setColumns((columns) => ({
        ...columns,
        [newIndex]: {
          ...columns[newIndex],
          items: result
        }
      }));

      console.log("set colmns", columns);

      // setColumns((items) => {
      //   console.log('items', items)
      //   const oldIndex = items[activeContainer].items.findIndex(({id}) => id === active.id);
      //   const newIndex = items[overContainer].items.findIndex(({id}) => id === over.id);

      //   console.log('colnew', columns)
      //   console.log('oldindex', oldIndex)
      //   console.log('newindex', newIndex)
      //   return arrayMove(items[overContainer], oldIndex, newIndex);
      // });
    }
  };

enter image description here

enter image description here

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

0

Try updating your setColumns code to following mentioned code:

setColumns((columns) => ({
        ...columns,
        [activeContainer]: {
          ...columns[activeContainer],
          items: result
        }
      }));

As per my understanding, the data was not correctly updated in the states.

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