React for JavaScript Programmers III: Spread them!
One thing we have talked several times before is the immutability of original data in functional programming. This is somewhat problematic in JavaScript.
One problem with JavaScript is when you assign an array, you actually copy the reference, not made a copy.
For example, if you have an array called myArray, and you want to make a copy, you would PROBABLY do...
var myArray=[1,2,3,4]
var myCopy = myArray
However, JavaScript did NOT create a new array here. Instead, both myArray and myCopy are pointing to the SAME array. So changing either myArray or myCopy will "mutate" the original array.
You can explicitly create a new array with an array construction array()
let myCopy = new Array()
But how do you copy stuff over? You can copy it with the from() method
let myCopy = Array.from(myArray)
What if we want to copy from multiple arrays? We could use concat()
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2); // [a,b,c,d,e,f]
One problem with JavaScript is when you assign an array, you actually copy the reference, not made a copy.
For example, if you have an array called myArray, and you want to make a copy, you would PROBABLY do...
var myArray=[1,2,3,4]
var myCopy = myArray
However, JavaScript did NOT create a new array here. Instead, both myArray and myCopy are pointing to the SAME array. So changing either myArray or myCopy will "mutate" the original array.
You can explicitly create a new array with an array construction array()
let myCopy = new Array()
But how do you copy stuff over? You can copy it with the from() method
let myCopy = Array.from(myArray)
What if we want to copy from multiple arrays? We could use concat()
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2); // [a,b,c,d,e,f]
Gee, how many variations are there? Quite a few.
But there's a much easier way... with the spread operator, that replaces all these, and can be used on arrays, objects, and more.
So this:
var myArray=[1,2,3,4]
var myCopy = Array.from(myArray)
var myCopy = Array.from(myArray)
Becomes this instead:
var myArray=[1,2,3,4]
var myCopy = [...myArray]
Concatenating arrays is now easy-peasy:
const array3=[...array1,...array2]
And you can insert arbitrary entries in there without problems.
const array3=[...array1,42,...array2,7]
This also works on objects (and other things)! (And thus, React states!)
const activeUsers= [
{user: "John", rank: "user"},
{user: "Jean",rank:"admin"}
]
function addUser(newUser) {
return [...activeUsers, {...newUser}]
}
newActiveUsers = addUser({user: "Joey",rank: "Intern"})
And now we can setState with this newActiveUsers object!
There are obviously other uses for the spread operator, but it's mostly used in React to help you copy the existing state with new stuff added.
For a full explanation of spread operator, see Colt Steele's short video.
But wait, we have much more about React to cover! Next up... props and states and contexts and so on....
var myCopy = [...myArray]
Concatenating arrays is now easy-peasy:
const array3=[...array1,...array2]
And you can insert arbitrary entries in there without problems.
const array3=[...array1,42,...array2,7]
const activeUsers= [
{user: "John", rank: "user"},
{user: "Jean",rank:"admin"}
]
function addUser(newUser) {
return [...activeUsers, {...newUser}]
}
newActiveUsers = addUser({user: "Joey",rank: "Intern"})
And now we can setState with this newActiveUsers object!
There are obviously other uses for the spread operator, but it's mostly used in React to help you copy the existing state with new stuff added.
For a full explanation of spread operator, see Colt Steele's short video.
But wait, we have much more about React to cover! Next up... props and states and contexts and so on....
Comments
Post a Comment