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

143
Vistas
Can't update value using reducer React JS after click

I'm currently learning useReducer and try to convert useState to useReducer in todo app. My problem :

  1. TOGGLE_TODO can't update the value when click.
case TOGGLE_TODO:
   let targetId = todoItem[action.index];
   let newTodo = [...todoItem];
   return (newTodo[targetId].completed = !newTodo[targetId].completed);
<button
    value={index}
    onClick={(event) =>
       dispatch({
          type: TOGGLE_TODO,
          index: event.target.value,
       })
    }
 >
    {todo.completed ? "done" : "pending"}
 </button>
  1. UPDATE_TODO, I have no clue for convert this to useReducer. Can I convert this too ? Here is my code using useState.
const onUpdate = (e) => {
   const target = e.currentTarget.value;
   const todoTarget = todoItem[target].name;
   setInput(todoTarget);
   setTodoIndex(target);
};

And here is my codesandbox for full code. using useState and using useReducer

about 4 years ago · Juan Pablo Isaza
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