Update single value in item array | react redux

前端 未结 3 1893
南旧
南旧 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条回答
  •  被撕碎了的回忆
    2020-12-01 02:43

    You need to transform your todos array to have the appropriate item updated. Array.map is the simplest way to do this:

    case "COMPLETE_TASK":
        return {
            ...state,
            todos: state.todos.map(todo => todo.id === action.id ?
                // transform the one with a matching id
                { ...todo, completed: action.completed } : 
                // otherwise return original todo
                todo
            ) 
        };
    

    There are libraries to help you with this kind of deep state update. You can find a list of such libraries here: https://github.com/markerikson/redux-ecosystem-links/blob/master/immutable-data.md#immutable-update-utilities

    Personally, I use ImmutableJS (https://facebook.github.io/immutable-js/) which solves the issue with its updateIn and setIn methods (which are more efficient than normal objects and arrays for large objects with lots of keys and for arrays, but slower for small ones).

提交回复
热议问题