Skip to content

Commit

Permalink
🧱 Destructuring Props and State
Browse files Browse the repository at this point in the history
Destructuring Props and State
  • Loading branch information
SaishJ committed Aug 22, 2022
1 parent 80afb6d commit 502b59a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import Counter from "./components/counter";
function App() {
return (
<div className="App">
<Counter />
{/* <Message /> */}
{/* <Counter /> */}
<Message />
{/* Functional Components props */}
{/* <Greet name="Saish" nickName="SJ" />
<Greet name="Pratik" nickName="PZ" />
<Greet name="Saish" nickName="SJ" />
{/* <Greet name="Pratik" nickName="PZ" />
<Greet name="Prafulla" nickName="Papya" /> */}
{/* Class Component props */}
{/* <Welcome name="Shivam" nickName="SA">
<button>Click Me</button>
</Welcome> */}
<Welcome name="Shivam" nickName="SA">
{/* <button>Click Me</button> */}
</Welcome>
{/* <Hello /> */}
</div>
);
Expand Down
20 changes: 17 additions & 3 deletions src/components/Greet.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import React from "react";

const Greet = (props) => {
console.log(props);
const Greet = ({ name, nickName }) => {
//Destructure prop in function parameter.

// const { name, nickName } = props; //Destructure prop in function body.
return (
<div>
<h1>
Hello {props.name} a.k.a {props.nickName}
Hello {name} a.k.a {nickName}
</h1>
</div>
);
};

export default Greet;

// Destructure prop in function component.
// 1) pass prop in function parameter
// const Greet = ({name, nickName}) => {
// return()
// }

// 2) pass prop in function body
// const Greet =(props) => {
// const (name, nickName) = props;
// return()
// }
3 changes: 2 additions & 1 deletion src/components/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class Message extends Component {
}

render() {
const { Message } = this.state; //Destructure state.
return (
<div>
<h1>{this.state.Message}</h1>
<h1>{Message}</h1>
<button onClick={() => this.changeMessage()}>Click Me</button>
</div>
);
Expand Down
7 changes: 5 additions & 2 deletions src/components/Welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import React, { Component } from "react";

class Welcome extends Component {
render() {
const { name, nickName } = this.props;
return (
<div>
<h1>Hello {this.props.name}</h1>
{this.props.children}
<h1>
Hello {name} a.k.a {nickName}
</h1>
{/* {this.props.children} */}
</div>
);
}
Expand Down

0 comments on commit 502b59a

Please sign in to comment.