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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
WordPress: How to print ACF array field values?WordPressKubernetes vs. Docker? It’s a misleading phraseMiscellaneousWordPress: How to add a Search Icon in Menus with toggle effect using jQuery?jQueryHow to detect if browser supports WebP format on server side PHP script?PHPHow to check if the page is the home page in WordPress?WordPressHow can one check to see if a remote file exists using PHP?PHP