Update single value in item array | react redux

前端 未结 3 1888
南旧
南旧 2020-12-01 02:13

I have a todo list and want to set the state of that item in the array to \"complete\" if the user clicks on \"complete\".

Here is my action:

export         


        
3条回答
  •  -上瘾入骨i
    2020-12-01 03:00

    New state does no longer have the text associated of that todo item on the selected item and the ID is no longer there, Is this because I am overwriting the state and ignoring the previous properties?

    Yes, because during each update you are assigning a new array with only one key completed, and that array doesn't contain any previous values. So after update array will have no previous data. That's why text and id's are not there after update.

    Solutions:

    1- Use array.map to find the correct element then update the value, Like this:

    case "COMPLETE_TASK":
        return {
            ...state,
            todos: state.todos.map(todo => 
                todo.id === action.id ? { ...todo, completed: action.completed } : todo
            ) 
        };
    

    2- Use array.findIndex to find the index of that particular object then update that, Like this:

    case "COMPLETE_TASK":
        let index = state.todos.findIndex(todo => todo.id === action.id);
        let todos = [...state.todos];
        todos[index] = {...todos[index], completed: action.completed};
        return {...state, todos}
    

    Check this snippet you will get a better idea about the mistake you are doing:

    let state = {
      a: 1,
      arr: [
        {text:1, id:1, completed: true},
        {text:2, id:2, completed: false}
      ]
    }
    
    console.log('old values', JSON.stringify(state));
    
    // updating the values
    
    let newState = {
       ...state,
       arr: [{completed: true}]
    }
    
    console.log('new state = ', newState);

提交回复
热议问题