Given that I have never worked on Angular or React before, I was chose as a candidate for learning react from scratch. I have been working with React for 2 quarters now and I still see so many unknown syntaxes and functionalities.
This deals with assigning array values to individual variables in very simple manner as follow:
const foo = [‘one’, ‘two’, ‘three’];
const [red, yellow, green] = foo;
console.log(red); // “one”
console.log(yellow); // “two”
console.log(green); // “three”
const [count, setCount] = useState(0);
This deals with extracting some of the values from an array into a few variables and then keeping rest of them as is. Going through this will probably explain it better than me.
In other words, if you have an array of 10 values and want to only separate 2 values out of it for use keep the rest of them where they came from, you use the spread operator. Look at the example below:
const [a, …b] = [1, 2, 3];
console.log(a); // 1
console.log(b); // [2, 3]
Overall, it is fun learning these new syntaxes and using them in day to day work. Will keep learning until I totally understand it and feel like a pro 🙂
Thank you for reading.