我在我的redux应用程序中使用Immutable JS并尝试更新我的对象中存在的数组,我只能在每次后续添加后替换之前添加一个项目。
restart = input("\nDo you want to restart the program? [y/n] > ")
if str(restart) == str("y"):
os.execl(sys.executable, sys.executable, * sys.argv) # Nothing hapens
else:
print("\nThe program will be closed...")
sys.exit(0)
州:
/**************
INITIAL STATE
***************/
const INITIAL_STATE = Immutable.fromJS({
budgetCategories: [
{budgetName: 'budgetName', monthlyCost: '$200.00', rollOverEnabled: true, dueDate: 'monthly'}
]
});
/**************
TYPES
***************/
export const ADD_BUDGET = 'src/Budget/ADD_BUDGET';
/**************
REDUCER LOGIC FLOW
***************/
export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case ADD_BUDGET:
console.log("Payload is:");
console.log(action.payload);
return INITIAL_STATE.updateIn(['budgetCategories'], arr => arr.push(Immutable.fromJS(action.payload)))
default:
return state;
}
}
运行reducer函数并更改budgetName只会产生:
Index 0:
budgetName(pin): "rwer"
monthlyCost(pin): "tryrtyt"
rollOverEnabled(pin): false
dueDate(pin): "Sat, 06 Jan 2018 17:24:44 GMT"
如您所见,此代码会覆盖索引。
答案 0 :(得分:0)
返回状态而不是 INITIAL_STATE :
return state.updateIn(['budgetCategories'], arr =>
arr.push(Immutable.fromJS(action.payload))
);