Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!