Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Why isn't the edit task function working for my function?

I'm working on a React project where I have a list of tasks that are able to be "edited". My thought process is when the user clicks edit, it fills the form with the current tasks information, and when they click "save edit" it just finds the key at which the task is at, deletes it, then creates a new task with a new key, but all of the other information is the same. However, when I try this, it does not seem to work, and just deletes the task.

Heres a snippet, hopefully it helps:

var saveHandler = (e) => {
    e.preventDefault();
    props.seteId(props.editTask.id); //set the old task's key
    props.setTasks([ //add our "edited" task to the list with the form info, and new key.
        ...props.tasks,
        {
          text: props.inputText,
          status: props.status,
          id: Math.random() * 1500, //I know this isnt a great way to do it
          priority: props.priority,
        },
      ]);

     props.setTasks(props.tasks.filter((el) => el.id !== props.eId)); //find old task and delete it 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

State is updated asynchronously.

You're calling props.setTasks twice, but the value of props.tasks isn't changing between them so you are overwriting (not building on) the first state change you make.

Use a variable to store your intermediate state instead.

const allTasks = [ ...props.tasks, { /* etc */ } ];
const updatedTasks = allTasks.filter( /* etc */ );
props.setTasks(updatedTasks);

Likewise, props.eId won't have updated in time for el.id !== props.eId so use props.editTask.id instead of props.eId in your filter function.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If i wel understand your needs this should fix your issue, testing using the old id props.editTask.id

var saveHandler = (e) => {
    e.preventDefault();
    props.seteId(props.editTask.id); //set the old task's key
    props.setTasks([ //add our "edited" task to the list with the form info, and new key.
        ...props.tasks,
        {
          text: props.inputText,
          status: props.status,
          id: Math.random() * 1500, //I know this isnt a great way to do it
          priority: props.priority,
        },
      ]);

     props.setTasks(props.tasks.filter((el) => el.id !== props.editTask.id)); //find old task and delete it

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda