Immutable Arrays

Copy an array…

clone = array => [...array]

Add something to the end of an array…

push = array => [...array, thing]

Remove the last item in an array…

pop = array => array.slice(0,1)

…or the first item…

shift = array => array.slice(1)

or an item at a specific index…

delete = i => array => [...array.slice(0,i), ...array.slice(i+1)]

If you are unsure whether a method mutates or not then checkout doesitmutate.xyz

Thank you Luke Jackson.

 
2
Kudos
 
2
Kudos

Now read this

React setState takes an updater function

If you increment a count in state using a state update function 3 times then the final count will be 3. state = {count: 0} this.setState( previousState => ({count: previousState.count++})) this.setState( previousState => ({count:... Continue →