Component composition helps you build complex UIs by combining small, simple pieces. It makes your code easier to understand and reuse.
Component composition in React
function Parent() { return ( <Child> <GrandChild /> </Child> ); } function Child({ children }) { return <div>{children}</div>; }
Use children prop to pass nested components inside another component.
Components can be functions that return JSX elements.
Card component wrapping some content passed as children.function Card({ children }) { return <div className="card">{children}</div>; } function App() { return ( <Card> <h2>Title</h2> <p>This is inside the card.</p> </Card> ); }
Toolbar composes two Button components with different labels and actions.function Button({ label, onClick }) { return <button onClick={onClick}>{label}</button>; } function Toolbar() { return ( <div> <Button label="Save" onClick={() => alert('Saved!')} /> <Button label="Cancel" onClick={() => alert('Canceled!')} /> </div> ); }
function Layout({ header, content, footer }) { return ( <div> <header>{header}</header> <main>{content}</main> <footer>{footer}</footer> </div> ); } function Page() { return ( <Layout header={<h1>Welcome</h1>} content={<p>This is the main content.</p>} footer={<small>Ā© 2024</small>} /> ); }
This React app shows how Container wraps other components and content. The Greeting component is composed inside Container along with a paragraph.
import React from 'react'; function Container({ children }) { return <section style={{ border: '2px solid #4a90e2', padding: '1rem', borderRadius: '0.5rem' }}>{children}</section>; } function Greeting({ name }) { return <h2>Hello, {name}!</h2>; } function App() { return ( <Container> <Greeting name="Alice" /> <p>Welcome to the component composition example.</p> </Container> ); } export default App;
Always use semantic HTML elements inside components for better accessibility.
Use the children prop to pass nested content easily.
Component composition helps keep your UI code clean and reusable.
Component composition means building UI by combining smaller components.
Use children or props to pass components inside others.
This approach makes your code easier to read, reuse, and maintain.
Practice
Solution
Step 1: Understand component composition
Component composition means creating complex UI by combining smaller, reusable components.Step 2: Compare options
Only To build UI by combining smaller components correctly describes this purpose. Others are incorrect or unrelated.Final Answer:
To build UI by combining smaller components -> Option DQuick Check:
Component composition = combining smaller components [OK]
- Thinking composition means one big component
- Confusing composition with performance optimization
- Ignoring the use of smaller components
Solution
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>.Step 2: Evaluate options
<Parent><Child /></Parent> uses correct JSX syntax. Options A, C, and D are invalid or incorrect ways to pass components.Final Answer:
<Parent><Child /></Parent> -> Option CQuick Check:
Children passed as JSX elements = <Parent><Child /></Parent> [OK]
- Passing children as plain text instead of components
- Using curly braces incorrectly around components
- Calling components like functions inside JSX
function Wrapper({ children }) {
return <div className="wrapper">{children}</div>;
}
function App() {
return (
<Wrapper>
<p>Hello</p>
<p>World</p>
</Wrapper>
);
}Solution
Step 1: Understand Wrapper component
Wrapper returns a div with class "wrapper" that renders its children inside.Step 2: Check children passed to Wrapper
App passes two <p> elements as children, so both appear inside the div.Final Answer:
<div class="wrapper"><p>Hello</p><p>World</p></div> -> Option AQuick Check:
Children rendered inside wrapper div = <div class="wrapper"><p>Hello</p><p>World</p></div> [OK]
- Thinking children must be a single element
- Expecting children to concatenate as text
- Ignoring the wrapper div output
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'));Solution
Step 1: Review Container component
Container returns a div wrapping props.children, which can be multiple elements.Step 2: Check usage in App
App passes two headings as children, which is valid JSX inside a div.Final Answer:
No error; this is valid composition -> Option AQuick Check:
Multiple children inside a div is valid JSX [OK]
- Thinking multiple children need a fragment wrapper
- Confusing React rules about keys with composition errors
- Assuming props.children must be a single element
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?Solution
Step 1: Understand Card component needs
Card should render header, children content, and footer separately for reuse and clarity.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.Final Answer:
Use props.header and props.footer to render header and footer, and props.children for content inside a container div -> Option BQuick 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]
- Passing header/footer as children causing confusion
- Ignoring children prop for main content
- Mixing content and layout in children prop
Ready to prove it?
Pass the test and this pattern moves unknown
