2016-09-16 95 views
1

有这样的状态:如何返回Redux的减速器的对象数组状态

review: { 
    "amount": 217, 
    "snippets": [ 
     { 
     "id": 0, 
     "flag": false 
     }, 
     { 
     "id": 1, 
     "flag": false 
     }, 
     ... 
    ] 
    } 

现在,如果我只是想设置review.snippets [1] .flag =真,什么应该写在减速机? 例如:

case SET_FLAG: return { 
    ...state, 
    review: { 
     ...state.review, 
     snippets: { 
     ...state.review.snippets, 
     // don't know how to write here to express array 
     } 
    }, 
}; 

回答

0

如果你不使用combineReducers分开您的商店的作品,你可以让你的减速情况类似,但做这样的操作给片断阵列

// grab your snippets 
const snippets = state.review.snippets 

// create a copy 
const snippetsCopy = snippets.slice() 

// replace the element with a new object with the same properties 
// and overwrite the property you want 
snippetsCopy[1] = { 
    ...snippetsCopy[1], 
    flag: true 
} 
const newSnippetsArray = Object.assign(arr, newArr) 

// in your switch case return something like this: 

return { 
    ...state, 
    review: { 
     ...state.review, 
     snippets: newSnippetsArray 
    }, 
};