Props for Reusable Components
Using Props to Make Reusable Components
Props is a way to pass data from a parent component down to a child component, and in the process allows us to create components that are more and more reusable.
We can pass properties & values (as arguments) to props  parameter of a functional component.
import React from "react"
// Child component
function ContactCard(props) {
    return (
        <div className="contact-card">
            <img src={props.imgUrl}/>
            <h3>{props.name}</h3>
            <p>Phone: {props.phone}</p>
            <p>Email: {props.email}</p>
        </div>
    )
}
export default ContactCardimport React from "react"
import ContactCard from "./ContactCard"
// Parent component
function App() {
    return (
        <div className="contacts">
            <ContactCard 
                name="Mr. Whiskerson" 
                imgUrl="http://placekitten.com/300/200" 
                phone="(212) 555-1234" 
                email="mr.whiskaz@catnap.meow"
            />
            // name, imgUrl, phone and email are properties (props) of the `ContactCard` component
            
            <ContactCard 
                name="Fluffykins" 
                imgUrl="http://placekitten.com/400/200" 
                phone="(212) 555-2345" 
                email="fluff@me.com"
            />
            
            <ContactCard 
                name="Destroyer" 
                imgUrl="http://placekitten.com/400/300" 
                phone="(212) 555-3456" 
                email="ofworlds@yahoo.com"
            />
            
            <ContactCard 
                name="Felix" 
                imgUrl="http://placekitten.com/200/100" 
                phone="(212) 555-4567" 
                email="thecat@hotmail.com"
            />
            
        </div>
    )
}
export default Appimport React from "react"
import ReactDOM from "react-dom"
import App from "./App"
ReactDOM.render(<App />, document.getElementById("root"))<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>We can also pass JS objects (as arguments) to props parameter of a functional component.
import React from "react"
// Child component
function ContactCard(props) {
    return (
        <div className="contact-card">
            <img src={props.contact.imgUrl}/>
            <h3>{props.contact.name}</h3>
            <p>Phone: {props.contact.phone}</p>
            <p>Email: {props.contact.email}</p>
        </div>
    )
}
export default ContactCardimport React from "react"
import ContactCard from "./ContactCard"
// Parent component
function App() {
    return (
        <div className="contacts">
            <ContactCard 
                contact={{name: "Mr. Whiskerson", imgUrl: "http://placekitten.com/300/200", phone: "(212) 555-1234", email: "mr.whiskaz@catnap.meow"}}
            />
            
            <ContactCard 
                contact={{name: "Fluffykins", imgUrl: "http://placekitten.com/400/200", phone: "(212) 555-2345", email: "fluff@me.com"}}
            />
            
            <ContactCard
                contact={{name: "Destroyer", imgUrl: "http://placekitten.com/400/300", phone: "(212) 555-3456", email: "ofworlds@yahoo.com"}}
            />
            
            <ContactCard 
                contact={{name: "Felix", imgUrl: "http://placekitten.com/200/100", phone: "(212) 555-4567", email: "thecat@hotmail.com"}}
            />
            
        </div>
    )
}
export default Appimport React from "react"
import ReactDOM from "react-dom"
import App from "./App"
ReactDOM.render(<App />, document.getElementById("root"))<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>Props & Inline Styling
We can use ternary operator  or short circuit operator to style components inline.
import React from "react"
function JokeItem(props){
    return(
        <div>
            // Styling with Ternary Operator 
            <h3 style={{display:props.question ? "block" : "none"}}>Joke Ques : {props.question}</h3>
            
            // Styling with Short Circuit Evaluation
            <p style={{color:!props.question && "#ccc"}}>Joke punchline: {props.punchLine}</p>
        </div>
    )
}
export default JokeItemimport React from "react"
import JokeItem from "./JokeItem"
function App(){
    return(
        <div className="jokes">
           
            <JokeItem punchLine="It’s hard to explain puns to kleptomaniacs because they always take things literally." />
            
            <JokeItem 
                question="What's the best thing about Switzerland?" 
                punchLine="I don't know, but the flag is a big plus!"
            />
            
            <JokeItem
                question="Did you hear about the mathematician who's afraid of negative numbers?" 
                punchLine="He'll stop at nothing to avoid them!"
            />
            
            <JokeItem 
                question="Hear about the new restaurant called Karma?" 
                punchLine="There’s no menu: You get what you deserve."
            />
            
        </div>
    )
}
export default Appimport React from "react"
import ReactDOM from "react-dom"
import App from "./App"
ReactDOM.render(
  <App />,
  document.getElementById('root')
)<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>Default Props
While most React components receive their props from a parent component, it's also possible to specify default props.  defaultProps allows us to set default, or fallback values for our component props .
import React from "react"
// Card component
function Card(props){
    const styles = {
        backgroundColor: props.cardColor,
        height: props.height,
        width: props.width
    }
    
    return(
        <div style={styles}></div>
    )
}
// Set default props
Card.defaultProps = {
    cardColor: "red",
    height: 150,
    width: 150
}
export default Cardimport React from "react"
import Card from "./Card"
function App() {
    return (
        <div>
            // Component taking custom props value
            <Card cardColor="yellow" height={50} width={50} />
            
            // Component taking default props value
            <Card />
            
            // Component taking a mixture of default+custom props value
            <Card cardColor="green" />
        </div>
    )
}
export default Appimport React from "react"
import ReactDOM from "react-dom"
import App from "./App"
ReactDOM.render(<App />, document.getElementById("root"))
<html>
    <head>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>Prop Types
 To run type-checking on the props for a component, we can assign the special propTypes property.
import React from "react"
import PropTypes from "prop-types"
// Card component
function Card(props) {
    const styles = {
        backgroundColor: props.cardColor,
        height: props.height,
        width: props.width
    }
    
    return (
        <div style={styles}></div>
    )
}
// Define prop types
Card.propTypes = {
    // Ensuring prop is limited to specific values by treating it as an enum.
    cardColor: PropTypes.oneOf(["red", "blue"]).isRequired,
    
    // Declaring a prop as a specific JS type
    height: PropTypes.number,
    
    // Chaining with isRequired to make sure a warning is shown if the prop isn't provided
    width: PropTypes.number.isRequired
}
// Set default props
Card.defaultProps = {
    height: 150,
    width: 150
}
export default Card
import React from "react"
import Card from "./Card"
function App() {
    return (
        <div>
            <Card cardColor="red" height={200} width={200} />
            <Card cardColor="blue" />
            <Card cardColor="red" height={100} width={100} />
        </div>
    )
}
export default Appimport React from "react"
import ReactDOM from "react-dom"
import App from "./App"
ReactDOM.render(<App />, document.getElementById("root"))
<html>
    <head>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div id="root"></div>
        <script src="index.pack.js"></script>
    </body>
</html>Last updated
Was this helpful?