I got this insane problem that I think has more to do about pure JS rather than redux.
My initial state looks like this:
let data = {
outputs : ["0", "0", "0", "0"],
analogIn : "0"
}
and this is part of my reducer so when I try to change a value of the outputs array (eg the 3rd value)
let newstate = Object.assign({}, state);
switch (action.type) {
case 'CLICK_OUTPUT':
let outputs = newstate.outputs
let status = outputs[action.num]
let newStatus = (status == "0") ? "1" : "0"
outputs[action.num] = newStatus
newstate.outputs = outputs
console.log(newstate.outputs[action.num]) <-- 1.
console.log(newstate.outputs) <-- 2.
console.log(newstate) <-- 3.
return newstate
default:
return state || data.data
}
Returns
analogIn : "0", outputs : ["0", "0", "0", "0"]
so the state is not changed...
When I try to change the analogIn it works
case 'CHANGE_IN':
newstate.analogIn = action.val
return newstate
Any ideas why does this happen? Does it have to do with redux?
Ok, found the solution.
As it seems using this:
newstate.outputs = outputs
or this:
newstate.outputs[num] = "1"
does not work if you want to copy the contents of one array to the value of an object that is an array
The solution was to clone the array
newstate.outputs = outputs.slice(0)
I'm not sure if this occured because of es6 or it is pure JS.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.