Skip to main content

Command Palette

Search for a command to run...

40) Children Props

Updated
View as Markdown
  1. In React, children props is a special prop that allows you to pass components, elements, or content between opening and closing tags of a component. It’s what makes React components flexible and reusable.
    Normally, props are passed like this,

    <Component name="Rahul" />
    

    But with children, you pass content like this

    <Component>
      <h1>Hello World</h1>
    </Component>
    

    Everything inside <Component>...</Component> becomes props.children

  2. How It Works

    function Wrapper(props) {
      return <div>{props.children}</div>;
    }
    
    <Wrapper>
      <p>This is inside Wrapper</p>
    </Wrapper>
    
  3. Common Use Cases
    i) Layout Components

    function Card({ children }) {
      return <div className="card">{children}</div>;
    }
    
    <Card>
      <h2>Product</h2>
      <p>Price: ₹100</p>
    </Card>
    

    ii) Wrapper Components
    Used to apply styles or logic around content.
    iii) Reusable UI Patterns
    Instead of hardcoding content inside a component, you make it dynamic using children.

40) Children Props