Props stand for "Properties." They are read-only components. It is an object which stores the value of attributes of a tag and work similar
to the HTML attributes. Prop is a way to pass data from one component to other components. It is similar to function arguments. Props are
passed to the component in the same way as arguments passed in a function. Inside the components, we can add an attribute called props.
Props in functional component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'
const Greet = (props) => {
console.log(props)
return (
<div>
<h1>Hello {props.name} a.k.a {props.heroName} </h1>
{props.children}
</div>
)
}
export default Greet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
const Greet = (props) => { | |
console.log(props) | |
return ( | |
<div> | |
<h1>Hello {props.name} a.k.a {props.heroName} </h1> | |
{props.children} | |
</div> | |
) | |
} | |
export default Greet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
Import Greet from ‘./Greet | |
class App extends Component{ | |
render(){ | |
return( | |
<div> | |
<Greet name="Bruce" heroName="Batman"> | |
<p>This is a children props</p> | |
</Greet> | |
<Greet name="Clark" heroName="Superman"/> | |
<Greet name="Diana" heroName="Wonder woman"/> | |
</div> | |
); | |
} | |
} | |
export default App; |
Props in class component
These attributes are available in the component as this.props and can be used to render dynamic data in our render method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
class Welcome extends Component { | |
render() { | |
console.log(this.props) | |
return <h1>Welcome {this.props.name} a.k.a {this.props.heroName}</h1> | |
} | |
} | |
export default Welcome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
Import Welcome from ‘./Welcome | |
class App extends Component{ | |
render(){ | |
return( | |
<div> | |
<Welcome name="Bruce" heroName="Batman"/> | |
<Welcome name="Clark" heroName="Superman"/> | |
<Welcome name="Diana" heroName="Wonder woman"/> | |
</div> | |
); | |
} | |
} | |
export default App; |
Passing object as props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import Car from './Car' | |
class Garage extends Component { | |
render() { | |
const carinfo = {name: "Ford", model: "Mustang"} | |
return ( | |
<div> | |
<h1>Who lives in my garage?</h1> | |
<Car brand={carinfo}/> | |
</div> | |
) | |
} | |
} | |
export default Garage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
class Car extends Component { | |
render() { | |
return ( | |
<div> | |
<h2>I am a {this.props.brand.model}!</h2> | |
</div> | |
) | |
} | |
} | |
export default Car |
change over time. We can achieve this using state.
Destructuring Props
It makes it possible to unpack the values from arrays or properties from objects into distinct variables.
Class component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
class Welcome extends Component { | |
render() { | |
const {name, heroName} = this.props | |
return <h1>Welcome {name} a.k.a {heroName}</h1> | |
} | |
} | |
export default Welcome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
Import Welcome from ‘./Welcome | |
class App extends Component{ | |
render(){ | |
return( | |
<div> | |
<Welcome name="Bruce" heroName="Batman"/> | |
<Welcome name="Clark" heroName="Superman"/> | |
<Welcome name="Diana" heroName="Wonder woman"/> | |
</div> | |
); | |
} | |
} | |
export default App; |
Functional component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Destructuring in the parameter | |
const Greet = ({name, heroName}) => { | |
return ( | |
<div> | |
<h1>Hello {name} a.k.a {heroName} </h1> | |
</div> | |
) | |
} | |
// Destructuring in the function body | |
const Greet = (props) => { | |
const {name, heroName} = props | |
return ( | |
<div> | |
<h1>Hello {name} a.k.a {heroName} </h1> | |
</div> | |
) | |
} | |
export default Greet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
Import Greet from ‘./Greet | |
class App extends Component{ | |
render(){ | |
return( | |
<div> | |
<Greet name="Bruce" heroName="Batman"> | |
<p>This is a children props</p> | |
</Greet> | |
<Greet name="Clark" heroName="Superman"/> | |
<Greet name="Diana" heroName="Wonder woman"/> | |
</div> | |
); | |
} | |
} | |
export default App; |
No comments:
Post a Comment