Introduction
In the previous article, we learned about the state and its usage, as well as the difference between props and state. Now, in this article, we will go ahead and review a small concept of destructuring props and states and the basics of event handling in React.js.
Destructuring
Destructuring is a simple concept introduced in React ES6. It is a JavaScript feature that allows users to extract multiple pieces of data from an array or object and assign them to their own variable. This concept allows us to extract data out of an array or object easily and makes your code readable and usable.
For example, you have an employee with multiple properties.
const employee = {
name: "emp",
salary: "10k",
designation: "tester"
}
Before ES6, you need to access properties as below.
console.log(employee.name);
console.log(employee.salary);
console.log(employee.designation);
Destructuring lets us distribute this code.
const { name, salary, designation } = employee;
The above code is the same as the code shown below.
const name = employee.name;
const salary = employee.salary;
const designation = employee.designation;
Now, the above properties can be accessed without using the employee. prefix.
console.log(name);
console.log(salary);
console.log(designation);
Destructuring props and states
Now, look at the example below. I want to pass by properties in the below-mentioned way.
const personalInfo = {
name: "xyz",
address: "150 Seattle",
zipcode: "111111",
city: "wc",
contactNumber: "999999999"
};
const professionalInfo = {
occupation: "business",
designation: "CEO",
salary: "50k"
};
Now, add the above properties in the Employee component in App.js.
import React, { Component } from "react";
import "./App.css";
import logo from "./logo.svg";
import Employee from "./Components/Employee";
class App extends Component {
render() {
const personalinfo = {
name: "xyz",
address: "150 Seattle",
zipcode: "111111",
city: "wc",
contactnumber: "999999999"
};
const proffessionalinfo = {
occupation: "business",
designation: "CEO",
salary: "50k"
};
return (
<p className="App">
<img src={logo} className="App-logo" alt="logo" />
<Employee personalinfo={personalinfo} proffessionalinfo={proffessionalinfo}>
</Employee>
</p>
);
}
}
export default App;
Access the above properties in the Employee component, as below.
import React from 'react';
const Employee = (props) => {
return (
<p>
<legend>Personal Details</legend><br/>
Name: {props.personalinfo.name}<br/>
Address: {props.personalinfo.address}<br/>
Zipcode: {props.personalinfo.zipcode}<br/>
City: {props.personalinfo.city}<br/>
Contact Number: {props.personalinfo.contactnumber}<br/>
<legend>Professional Detail</legend><br/>
Occupation: {props.professionalinfo.occupation}<br/>
Designation: {props.professionalinfo.designation}<br/>
Salary: {props.professionalinfo.salary}<br/>
</p>
);
}
export default Employee;



Bhawna KaushikPosted Jul 11, 2019, 6:23 AM
Very nice, thanks for sharing.
Mahesh VermaPosted Jul 9, 2019, 11:09 PM
Nice explanation......
Smi10 KalathiyaPosted Jul 9, 2019, 10:26 AM
Well explained..!!
Faisal PathanPosted Jul 5, 2019, 4:27 PM
Keep it up!
Aakash MauryaPosted Jul 5, 2019, 11:54 AM
Great one...