Yogesh Chauhan's Blog

How destructuring works in React?

in React on June 9, 2021

Destructuring is a small but a great concept that applies to arrays and objects both.

Destructuring feature was added in the ES6 specification of JavaScript.

In short, destructuring allows you to destructure values from objects and arrays.

Destructuring an object

In React, you pass the data via props to the components and use it like props.name, props.address via a dot operator.

Here’s an example of props object:

  props = {
    name: 'Yogesh Chauhan',
    address: 'Some Address',

Usually we use props like this:

  {props.name}'s address is {props.address}.

If you don’t want to use props.abc, props.xyz then you can just save the data into variables like this:

  let name = props.name;
  let address = props.address;

Now you can use those variables instead.

  {name}'s address is {address}.

That’s not it.

Destructuring makes your job easy when it comes to the assignment of variables.

You can just extract the values of an object’s properties and assign them to separate variables like this:

  let { name, address } = props

The order of those variables inside the bracket doesn’t matter!

That’s not it!

You can just directly pass that bracket with the variable names instead of props and it will work like a charm!

Let’s revisit the component helper function example.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How can one check to see if a remote file exists using PHP?PHPSafe Practice to add Links to cross-origin destinationUI/UXThe substr() method in JavaScript and how it’s different from substring()JavaScriptWhat is IPS(Intrusion Prevention System), How Does It Work and What are the Detection Types?MiscellaneousSQL Right JoinSQL/MySQLControl rendering using CSS content-visibility propertyCSS