React ES6 Destructuring

Imagine making a sandwich, you don’t pull everything out of the refrigerator, just the ingredients you need. Destructuring works the same way. When working with arrays or objects, you can extract only the items you need, making your code cleaner and more efficient.

Tutorials dojo strip

Destructuring Arrays

Here’s the old method of assigning array items to variables:

Before

JavaScript

Here’s the new method using destructuring:

With Destructuring

JavaScript

The order of the variables is crucial. If you only need the desktop and mobile, you can skip over the intermediate value:

JavaScript

Example

Destructuring is particularly useful when a function returns an array:

JavaScript

Destructuring Objects

Here’s the old way of using an object within a function:

Before

JavaScript

Here’s the new method with destructuring:

With Destructuring

JavaScript

Tutorials dojo strip