Nearby lessons
8 of 43⚛️ ReactJS – Destructuring Props
📌 What is Destructuring?
What is Destructuring in React?
Destructuring is a feature of JavaScript.
It is used to extract values from arrays or objects and assign them to new variables.
In React, it helps to directly extract props or state variables, making code cleaner and more readable.
In destructuring, it does not change an array or any object. It makes a copy of the desired object or array element by assigning them to new variables.
Later, we can use these new variables in React (class or functional) components.
📌 Without Destructuring
We can access array elements or props manually without destructuring.
📌 With Destructuring
We can extract array elements or props easily using destructuring.
📌 Skipping Array Elements
We can skip array elements by leaving empty spaces between commas during destructuring.
📌 Array Destructuring with Default Values
If the array doesn’t have enough values, we can set default values during destructuring.
📌 Object Destructuring with Renaming
We can rename variables while destructuring objects.
📌 Object Destructuring with Default Values
If props are missing, we can provide default values while destructuring.
📌 Accessing props directly
📌 Destructuring inside function body
📌 Destructuring in function parameter
📌 Wrong destructuring with different names
🧩 Advantages of Destructuring
- It makes developer’s life easy, by assigning their own variables.
- Improves readability and sustainability of code
- Reduces amount of code used in an application
- Trims steps needed to access data properties
- Provides components with exact data properties
- Saves time when iterating over arrays of objects