Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 11, 2022 05:22 am GMT

Destructuring React Quick Notes.

Remember basic meaning.

It means choosing only desire sweets from a cakeshop.

Here is an example to remember it.

const cakeVarieties = ['Chocolate','KitKat','Mango', 'Brownie'];const [myChoise1, myChoise2, ...rest] = cakeVarieties;console.log(myChoise1); //Chocolateconsole.log(myChoise2); //KitKatconsole.log(rest); //['Mango','Brownie'] --Remember rest means other things left and which is only be declared at last position.//Below is for #React.jsconst premiumCakeVarieties = {sameNameHere1: 'Chocolate', sameNameHere2:'KitKat', sameNameHere3:{fruitCakes :['Mango','Pineaple']},  sameNameHere4:'Brownie'};const {sameNameHere1, sameNameHere2, sameNameHere4, sameNameHere3 : {fruitCakes:[cake1, cake2]}} = premiumCakeVarieties; //Notice: We have same variable name no matter in which order they are they will access array property where matching name of itself.console.log(sameNameHere1); //Chocolateconsole.log(sameNameHere2); //KitKatconsole.log(sameNameHere4); //Brownieconsole.log(sameNameHere3); //['Mango','Pineaple']console.log(cake1); //Mango //this follows order here as we did not have key for itconsole.log(cake2); //Pineaple //this follows order here as we did not have key for it

Notes

  1. While working with an Array we can destruct with defining array left side.
const [a, b, ...rest] = [1,2,3,4]; //here an array both sides.
  1. While working with object we need brackets i.e. {}
const {a, b, ...rest} = obj; //here {} left side.

We only require this basics to work around, although we can check over https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment, which has best detail explanation and can be used when we destruct more complex object. But make all object simpler as possible.


Original Link: https://dev.to/ajaybaraiya6/destructuring-react-quick-notes-429e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To