javascript - How to delete an array in redux
PHP中文网
PHP中文网 2017-06-15 09:22:08
0
2
692

This is the initial content in my redux

Now I need to click on a list, for example, click Test 2, and I will delete this list

When you first enter, the content in the status is like this.

My judgment in redux is like this, but the result

It’s just that the parameters are gone, but the position of the array still exists. Is there any way to delete the corresponding array?

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
我想大声告诉你
return state.arr.filter(item => item.id !== id);
学霸

If you perform a map operation like this on any array, it will happen, leaving an array with holes. . .
The correct approach is

let newState = state.slice()
newState.splice(2, 1)
return newState

Every time you operate on the redux state, try to return a new object, such as

return Object.assign({}, oldState, newState)
return [...oldState, ...newState] // or [...oldState].concat(newState)

Looks like this...

The other thing is, if the empty position in your map does not specify a return value, then the default value returned is undefined. Why does it become null...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template