javascript - In redux reducer how to update 1 property inside array object -
i have reducer, first gets list of books array objects without book copies count property, , each book number of copies
const booklist= (state = [], action) => { switch (action.type) { case 'books_request_succeeded': return object.assign({}, state, action.payload); case 'books_count_request_succeeded': return updatebookcopiescount(state, action); default: return state } } const updatebookcopiescount = (state, action) => { const newstate = state.map((book) => { // updating book copies count if (book.id === action.payload.id) { return { ...book, copiescount: action.payload.copiescount }; } return book; }); return newstate; } my question is, right redux approach: should copy each time entire array objects each copiescount update, or ok copy object modified new property
thanks in advance
the current updatebookcopiescount function correct. need copy each level of nesting needs updated, only needs updated. so, need copy state (which being done via state.map(), , need copy just 1 object inside of array needs updated. other objects should returned as-is.
see structuring reducers - immutable update patterns section of redux docs more info.
Comments
Post a Comment