React native 嵌套异径管don';不能更新
我有一个关于嵌套减缩器的问题 结构与此类似:React native 嵌套异径管don';不能更新,react-native,redux,nested,reducers,React Native,Redux,Nested,Reducers,我有一个关于嵌套减缩器的问题 结构与此类似: const INITIAL_STATE = { items: [], planningState:null, loading: false, idx_selected : '2' }; const mockItems = [ { date: "2018-08-24 15:00:00", type: "dropoff", status: null, id: "553", //ma
const INITIAL_STATE = {
items: [],
planningState:null,
loading: false,
idx_selected : '2'
};
const mockItems = [
{
date: "2018-08-24 15:00:00",
type: "dropoff",
status: null,
id: "553",
//many others things
},
{
date: "2018-08-24 19:00:00",
type: "pickup",
status: "ordered",
id: "553",
//other things
},
{
date: "2018-07-18 08:00:00",
type: "delivery",
status: null,
id: "554",
//other things
},
在state.items中,结构如下所示:
const INITIAL_STATE = {
items: [],
planningState:null,
loading: false,
idx_selected : '2'
};
const mockItems = [
{
date: "2018-08-24 15:00:00",
type: "dropoff",
status: null,
id: "553",
//many others things
},
{
date: "2018-08-24 19:00:00",
type: "pickup",
status: "ordered",
id: "553",
//other things
},
{
date: "2018-07-18 08:00:00",
type: "delivery",
status: null,
id: "554",
//other things
},
])
我需要更改一个项目的状态,而不更改其他属性。我知道我必须克隆每一层,但我犯了一个错误
case SCAN_CLOSE_DONE:
//state.items[state.idx_selected].status=done
return{
...state,
items:{
...state.items,
[state.idx_selected]:{
...state.items[state.idx_selected],
status: "done"
}
}
};
以处理该状态块的所有内容的reducer为例,将其分解为多个reducer,每个reducer处理该状态块的一部分
const items = (state = [], { type, items, item, idx_selected, status }) => {
switch(type) {
case SCAN_CLOSE_DONE:
const newState = [...state];
const item = { ...newState[idx_selected], status };
newState[idx_selected] = item;
return newState;
//...
default:
return state;
}
};
// just examples, actual use case might be different:
const planningState = (state = null, { type, planningState }) => type === MY_ACTION ? planningState : state;
const loading = (state = null, { type, loading }) => type === ANOTHER_ACTION ? loading : state;
const idx_select = //...
export default combineReducers({
items,
planningState,
loading,
idx_select
});