React js Children Props Practice Questions with Solutions

Introduction

In React, children is a special prop used to access the content placed between the opening and closing tags of a component. It is especially useful when building reusable components such as cards, buttons, containers, modals, and layouts. Instead of passing every piece of content as a separate prop, we can place the content inside the component. In this chapter, you will practice the children prop through 10 solved questions. React js Children Props practice questions with solutions help to build concepts.

1. What is the children prop in React?

children is a special React prop that contains whatever content is placed between a component’s opening and closing tags.

Example:

function Box({ children }) {
  return <div>{children}</div>;
}

function App() {
  return (
    <Box>
      <h2>Hello React</h2>
    </Box>
  );
}

export default App;

Here:

<Box>
  <h2>Hello React</h2>
</Box>

The <h2> element becomes the children of the Box component.

Answer: The children prop allows a component to receive and display content placed inside it.


2. How do you access the children prop?

You can access children just like any other prop by using destructuring.

function Container({ children }) {
  return <div>{children}</div>;
}

function App() {
  return (
    <Container>
      <p>This is inside the container.</p>
    </Container>
  );
}

export default App;

The component receives:

{ children }

and displays it using:

{children}

Answer: Use {children} inside the component to access the content passed between its tags.


3. Can children contain text?

Yes. The children prop can contain simple text.

function Message({ children }) {
  return <h2>{children}</h2>;
}

function App() {
  return (
    <Message>
      Welcome to React.js
    </Message>
  );
}

export default App;

Output:

Welcome to React.js

Answer: Yes, children can contain plain text.


4. Can children contain JSX elements?

Yes. This is one of the most useful features of children.

function Card({ children }) {
  return (
    <div>
      {children}
    </div>
  );
}

function App() {
  return (
    <Card>
      <h2>React Course</h2>
      <p>Learn React from basics.</p>
      <button>Start Learning</button>
    </Card>
  );
}

export default App;

Here, the <h2>, <p>, and <button> elements are all passed as children.

Answer: Yes. children can contain JSX elements and multiple pieces of content.


5. What is the difference between a normal prop and children?

A normal prop is usually passed as an attribute:

<Student name="Rahul" />

The component can access it using:

function Student({ name }) {
  return <h2>{name}</h2>;
}

The children prop is passed between the component’s opening and closing tags:

<Card>
  <h2>React Course</h2>
</Card>

It can be accessed using:

function Card({ children }) {
  return <div>{children}</div>;
}

Answer: Normal props are commonly passed through attributes, while children represents the content placed inside a component.


6. Can we use children with other props?

Yes. A component can receive both normal props and children.

function Card({ title, children }) {
  return (
    <div>
      <h2>{title}</h2>
      <div>{children}</div>
    </div>
  );
}

function App() {
  return (
    <Card title="React.js Course">
      <p>Learn Components</p>
      <p>Learn Props</p>
      <p>Learn Hooks</p>
    </Card>
  );
}

export default App;

Here:

  • title is a normal prop.
  • children contains the three <p> elements.

Answer: Yes, children can be used together with normal props.


7. Can we pass a component as children?

Yes. One component can be passed inside another component as its children.

function Header() {
  return <h2>React Course</h2>;
}

function Box({ children }) {
  return (
    <div>
      {children}
    </div>
  );
}

function App() {
  return (
    <Box>
      <Header />
    </Box>
  );
}

export default App;

Here, <Header /> is passed as the child of <Box>.

Answer: Yes. Components can also be passed as children.


8. What happens if a component has no children?

If nothing is placed between the component’s opening and closing tags, the children value will be empty.

Example:

function Box({ children }) {
  return (
    <div>
      {children}
    </div>
  );
}

function App() {
  return <Box />;
}

export default App;

Since nothing is passed inside <Box />, there is no child content to display.

Answer: If no children are passed, the children prop has no content.


9. How can children make a component reusable?

Suppose we create a reusable Card component:

function Card({ children }) {
  return (
    <div>
      {children}
    </div>
  );
}

Now we can use the same component with different content:

function App() {
  return (
    <div>
      <Card>
        <h2>Student Card</h2>
        <p>Rahul is learning React.</p>
      </Card>

      <Card>
        <h2>Course Card</h2>
        <p>React.js is a popular UI library.</p>
      </Card>
    </div>
  );
}

export default App;

The Card component does not need to know what content it will receive.

Answer: children makes components flexible because the same component can display different content.


10. Build a reusable Panel component using children.

Create a reusable Panel component that accepts a title and any content through children.

function Panel({ title, children }) {
  return (
    <div>
      <h2>{title}</h2>

      <div>
        {children}
      </div>
    </div>
  );
}

function App() {
  return (
    <div>
      <Panel title="About React">
        <p>React is used to build user interfaces.</p>
      </Panel>

      <Panel title="React Topics">
        <ul>
          <li>Components</li>
          <li>Props</li>
          <li>State</li>
          <li>Hooks</li>
        </ul>
      </Panel>
    </div>
  );
}

export default App;

Here, the Panel component is reusable because different content can be placed inside it.

For example:

<Panel title="About React">
  <p>React is used to build user interfaces.</p>
</Panel>

and:

<Panel title="React Topics">
  <ul>
    <li>Components</li>
    <li>Props</li>
    <li>State</li>
  </ul>
</Panel>

Both use the same Panel component but display different children.

Answer: The children prop is useful for creating flexible and reusable components that can accept different content.

Key Takeaways

  • children is a special React prop.
  • It represents content placed between a component’s opening and closing tags.
  • children can contain text.
  • children can contain JSX elements.
  • Multiple elements can be passed as children.
  • Components can also be passed as children.
  • children can be used together with normal props.
  • If no content is passed, there are no children to render.
  • children helps create flexible and reusable components.
  • Common use cases include cards, panels, layouts, modals, buttons, and containers.

FAQs

1. What is the children prop in React?

The children prop contains the content placed inside a React component.

2. Is children a special prop in React?

Yes. React automatically provides the content between a component’s opening and closing tags through the children prop.

3. Can children contain multiple elements?

Yes. Children can contain multiple JSX elements.

4. Can we use children with normal props?

Yes. A component can receive normal props and children at the same time.

5. Can we pass a component as children?

Yes. A React component can be placed inside another component and received through children.

6. What happens when no children are passed?

If no content is placed inside the component, there is no child content to render.

7. Why are children useful in React?

Children make components more flexible and reusable because the component can accept different types of content without changing its internal structure.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top