代码片段理解:
[INCREMENT]: (state, action) => {
const { payload: { id } } = action //because payload contains the id and we already know that we are about
//to increment the value of that id, we modify only that value by one return {
...state,
counters: {
...state.counters,
[id]: state.counters[id] + 1
}
}
},
line 2:
const { payload: { id } } = action
相当于: id = action.payload.id
line 8 - 10:
{
8 ...state,
9 counters: {
10 ...state.counters,
11 [id]: state.counters[id] + 1
12 }
13 }
是表达:state.counter[id] = state.counters[id] + 1