📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will learn about destructuring props and states in react. The destructuring is an ES6 feature that makes it possible to unpack values from arrays or properties from objects into distinct variables. In React, destructuring props and states improve code readability.
What is Destructuring?
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
emailId: "ramesh@gmail.com"
}
console.log(employee .firstName) // Ramesh
console.log(employee .lastName) // Fadatare
console.log(employee .emailId) // ramesh@gmail.com
const { firstName, lastName, emailId } = employee ;
const firstName = employee .firstName
const lastName = employee .lastName
const emailId= employee .emailId
console.log(firstName) // Ramesh
console.log(lastName) // Fadatare
console.log(emailId) // ramesh@gmail.com
Destructuring props in Functional Components
Employee functional component
import React from 'react'
export const Employee = props => {
return (
<div>
<h1> Employee Details</h1>
<h2> First Name : {props.firstName} </h2>
<h2> Last Name : {props.lastName} </h2>
<h2> Eamil Id : {props.emailId} </h2>
</div>
)
}
App component
import React from 'react';
import logo from './logo.svg';
import './App.css';
import Table from './components/functional-components/Table';
import PropsDemo from './components/props/PropsDemo';
import { Employee } from './components/Employee';
function App() {
return (
<div className="App">
<header className="App-header">
<Employee firstName = "Ramesh" lastName = "Fadatare" emailId = "ramesh@gmail.com" />
</header>
</div>
);
}
export default App;
Two ways to destructure props in functional component
import React from 'react'
export const Employee = ({firstName, lastName, emailId}) => {
return (
<div>
<h1> Employee Details</h1>
<h2> First Name : {firstName} </h2>
<h2> Last Name : {lastName} </h2>
<h2> Eamil Id : {emailId} </h2>
</div>
)
}
import React from 'react'
export const Employee = props => {
const {firstName, lastName, emailId} = props;
return (
<div>
<h1> Employee Details</h1>
<h2> First Name : {firstName} </h2>
<h2> Last Name : {lastName} </h2>
<h2> Eamil Id : {emailId} </h2>
</div>
)
}
Destructuring props in class Components
import React, { Component } from 'react'
class Employee extends Component {
render() {
return (
<div>
<h1> Employee Details</h1>
<h2> First Name : {this.props.firstName} </h2>
<h2> Last Name : {this.props.lastName} </h2>
<h2> Eamil Id : {this.props.emailId} </h2>
</div>
)
}
}
export default Employee;
import React, { Component } from 'react'
class Employee extends Component {
render() {
const {firstName, lastName, emailId} = this.props;
return (
<div>
<h1> Employee Details</h1>
<h2> First Name : {firstName} </h2>
<h2> Last Name : {lastName} </h2>
<h2> Eamil Id : {emailId} </h2>
</div>
)
}
}
export default Employee;
Destructuring state
Destructuring states is similar to props. Here is syntax to Destructuring states in React:import React, { Component } from 'react' class StateDemp extends Component { render() { const {state1, state2, state3} = this.state; return ( <div> <h1> State Details</h1> <h2> state1 : {state1} </h2> <h2> state2 : {state2} </h2> <h2> state3 : {state3} </h2> </div> ) } } export default StateDemo;
What's Next?
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment