📖 Scenario: You are building a simple user profile page. The page shows a user's name and a short bio. To keep your code clean and reusable, you want to split the page into smaller pieces called components.
🎯 Goal: Create a React app with two components: UserName and UserBio. Then compose them inside a parent component called UserProfile to display the user's name and bio together.
📋 What You'll Learn
Create a UserName component that shows the user's name.
Create a UserBio component that shows the user's bio.
Create a UserProfile component that uses UserName and UserBio inside it.
Use React functional components and hooks if needed.
Use JSX syntax correctly.
💡 Why This Matters
🌍 Real World
Component composition is how React apps build complex user interfaces by combining small, reusable pieces. This makes apps easier to build and maintain.
💼 Career
Understanding component composition is essential for React developers. It is a core skill used in almost every React project in the industry.
Progress0 / 4 steps
1
Create the UserName component
Create a React functional component called UserName that returns an <h1> element with the text "Alice Johnson".
React
Hint
Remember, a React functional component is a function that returns JSX. Use function UserName() and return <h1>Alice Johnson</h1>.
2
Create the UserBio component
Create a React functional component called UserBio that returns a <p> element with the text "Frontend developer and coffee lover.".
React
Hint
Use the same pattern as UserName. Create a function UserBio that returns a paragraph with the bio text.
3
Create the UserProfile component
Create a React functional component called UserProfile that returns a <div> containing the UserName and UserBio components inside it.
React
Hint
Inside UserProfile, return a <div> that includes the UserName and UserBio components as JSX tags.
4
Render the UserProfile component
Add a React root render call that renders the UserProfile component inside an HTML element with the id root. Use ReactDOM.createRoot and root.render.
React
Hint
Use ReactDOM.createRoot(document.getElementById('root')) to create the root, then call root.render(<UserProfile />) to show the component.
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
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 D
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 A
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
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 B
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 unknownstrongwhile earning +1 Grip toward your certificate.