Bird
Raised Fist0
Reactframework~10 mins

Component composition in React - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Component composition
Parent Component
↓
Render Child Components
↓
Pass Props or Children
↓
Child Components Render
↓
Combine Outputs in Parent
The parent component renders child components, passing data or children, then combines their outputs to build the UI.
Execution Sample
React
function Button({label}) {
  return <button>{label}</button>;
}

function App() {
  return <Button label="Click me" />;
}
A parent component App renders a child Button component, passing a label prop to display.
Execution Table
StepComponentProps/ChildrenRender OutputParent Update
1Appnone<Button label="Click me" />App prepares to render Button
2Buttonlabel="Click me"<button>Click me</button>Button output ready
3AppButton output<button>Click me</button>App renders final output
4EndN/AUI shows a button with text 'Click me'Rendering complete
💡 All components rendered and combined, UI is ready.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
App JSXundefined<Button label="Click me" /><Button label="Click me" /><button>Click me</button><button>Click me</button>
Button propsundefinedundefined{label: "Click me"}{label: "Click me"}{label: "Click me"}
Button outputundefinedundefined<button>Click me</button><button>Click me</button><button>Click me</button>
Key Moments - 2 Insights
Why does the Button component receive props inside App?
Because App passes the label prop when rendering Button, as shown in execution_table step 1 and 2.
How does the parent component combine child outputs?
The parent uses the child's render output as part of its own JSX, seen in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Button component's render output at step 2?
A<Button label="Click me" />
B<button>Click me</button>
Cundefined
D<button>Submit</button>
💡 Hint
Check the 'Render Output' column for Button at step 2.
At which step does the App component have the final combined output?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for when App's 'Render Output' shows the button element.
If App passed label="Submit" instead, how would Button's output change at step 2?
A<button>Submit</button>
B<button>Click me</button>
C<button></button>
D<Button label="Submit" />
💡 Hint
Button output depends on the label prop passed by App, see variable_tracker for Button props.
Concept Snapshot
Component composition in React means building UI by nesting components.
Parent components render children and pass data via props.
Children render their own UI using props or children.
Parent combines child outputs to form the final UI.
This keeps code modular and reusable.
Full Transcript
Component composition in React is when a parent component renders child components inside it. The parent can pass data to children using props. Each child uses these props to render its own part of the UI. Then the parent combines all child outputs to build the full interface. For example, a parent App renders a Button component and passes a label prop. Button uses this label to show text inside a button element. The rendering happens step-by-step: App prepares Button with props, Button renders the button element, then App includes that output in its own render. This way, components stay small and reusable, making UI easier to build and maintain.

Practice

(1/5)
1. What is the main purpose of component composition in React?
easy
A. To make components run faster by skipping rendering
B. To write all UI code in a single component
C. To avoid using props between components
D. To build UI by combining smaller components

Solution

  1. Step 1: Understand component composition

    Component composition means creating complex UI by combining smaller, reusable components.
  2. Step 2: Compare options

    Only To build UI by combining smaller components correctly describes this purpose. Others are incorrect or unrelated.
  3. Final Answer:

    To build UI by combining smaller components -> Option D
  4. Quick Check:

    Component composition = combining smaller components [OK]
Hint: Think: How do you build complex UI simply? [OK]
Common Mistakes:
  • Thinking composition means one big component
  • Confusing composition with performance optimization
  • Ignoring the use of smaller components
2. Which of the following is the correct way to pass child components in React?
easy
A. Child
B. {Child}
C.
D. Child()

Solution

  1. Step 1: Identify correct JSX syntax for children

    In React, child components are passed inside the parent tags as JSX elements, like <Parent><Child /></Parent>.
  2. Step 2: Evaluate options

    <Parent><Child /></Parent> uses correct JSX syntax. Options A, C, and D are invalid or incorrect ways to pass components.
  3. Final Answer:

    <Parent><Child /></Parent> -> Option C
  4. Quick Check:

    Children passed as JSX elements = <Parent><Child /></Parent> [OK]
Hint: Children go inside parent tags as JSX elements [OK]
Common Mistakes:
  • Passing children as plain text instead of components
  • Using curly braces incorrectly around components
  • Calling components like functions inside JSX
3. What will be rendered by this React code?
function Wrapper({ children }) {
  return <div className="wrapper">{children}</div>;
}

function App() {
  return (
    <Wrapper>
      <p>Hello</p>
      <p>World</p>
    </Wrapper>
  );
}
medium
A.

Hello

World

B.

Hello

World

C.
HelloWorld
D. Error: children must be a single element

Solution

  1. Step 1: Understand Wrapper component

    Wrapper returns a div with class "wrapper" that renders its children inside.
  2. Step 2: Check children passed to Wrapper

    App passes two <p> elements as children, so both appear inside the div.
  3. Final Answer:

    <div class="wrapper"><p>Hello</p><p>World</p></div> -> Option A
  4. Quick Check:

    Children rendered inside wrapper div = <div class="wrapper"><p>Hello</p><p>World</p></div> [OK]
Hint: Children inside Wrapper appear inside its div container [OK]
Common Mistakes:
  • Thinking children must be a single element
  • Expecting children to concatenate as text
  • Ignoring the wrapper div output
4. Identify the error in this React component composition:
function Container(props) {
  return <div>{props.children}</div>;
}

function App() {
  return (
    <Container>
      <h1>Title</h1>
      <h2>Subtitle</h2>
    </Container>
  );
}

// Usage
ReactDOM.render(<App />, document.getElementById('root'));
medium
A. No error; this is valid composition
B. props.children must be wrapped in a fragment
C. Container must return a single child element
D. Cannot pass multiple children without keys

Solution

  1. Step 1: Review Container component

    Container returns a div wrapping props.children, which can be multiple elements.
  2. Step 2: Check usage in App

    App passes two headings as children, which is valid JSX inside a div.
  3. Final Answer:

    No error; this is valid composition -> Option A
  4. Quick Check:

    Multiple children inside a div is valid JSX [OK]
Hint: Multiple children inside a div are allowed without extra wrappers [OK]
Common Mistakes:
  • Thinking multiple children need a fragment wrapper
  • Confusing React rules about keys with composition errors
  • Assuming props.children must be a single element
5. You want to create a reusable Card component that accepts a header and footer as props and displays children content in the middle. Which is the best way to compose this component?
hard
A. Render header and footer inside children and pass content as a separate prop
B. Use props.header and props.footer to render header and footer, and props.children for content inside a container div
C. Pass header and footer as children and ignore props.children
D. Use only props.children and expect header/footer inside it

Solution

  1. Step 1: Understand Card component needs

    Card should render header, children content, and footer separately for reuse and clarity.
  2. Step 2: Evaluate composition options

    Use props.header and props.footer to render header and footer, and props.children for content inside a container div correctly uses props.header and props.footer for fixed parts and props.children for flexible content.
  3. Final Answer:

    Use props.header and props.footer to render header and footer, and props.children for content inside a container div -> Option B
  4. Quick Check:

    Separate header/footer props + children content = Use props.header and props.footer to render header and footer, and props.children for content inside a container div [OK]
Hint: Use props for fixed parts, children for flexible content [OK]
Common Mistakes:
  • Passing header/footer as children causing confusion
  • Ignoring children prop for main content
  • Mixing content and layout in children prop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard