Hlaing_Min_Than's avatar

React update state with reducer

i'm really confuse of update the array of objects with map function in reducer.please if u are familiar with react.can u explain to me .

let reducer = (todos, action) => {
  if (action.type === "toggle-complete-todo") {
    let newTodos = todos.map((todo) => {
      if (todo.id === action.payload.id) {
		// not update the complete property if we write like this
        //todo.complete = !todo.complete;
		return {...todo,complete:!todo.complete} //this will work
      }
      return todo; 
    });
    return newTodos;
  }
  return todos;
};
//state
let [todos, dispatch] = useReducer(reducer, [
    { id: 23434, name: "example todo", complete: true },
 ]);


0 likes
1 reply
Sinnbeck's avatar

I use react, but I don't see much of a question? Are you just looking for someone to explain reducers to you?

Please or to participate in this conversation.