Component Composition
Part of the React course
You have built a card with a border and a shadow, and it looks good. Now you need the same card around a photo, and around a login form, and around a list of comments. The tempting move is to copy the card three times, or to give it a prop for every possible thing it might contain: title, photoUrl, formFields, comments. Both roads end in a component nobody wants to touch.
The way out is composition: building big things by nesting small things inside each other, so that each piece stays ignorant of the others. A picture frame is a good way to hold this. The frame has a job, which is to put a border around whatever is inside it, and it does that job without knowing or caring whether it is holding a photograph, a certificate or a child's drawing. A well-built React component is a frame.
Want to learn React Component Composition in a real environment?
Click below, get a fresh Linux box with React ready to go, and work through Component Composition hands-on with an AI tutor that knows your code.
Start this lessonComponent CompositionWhat you'll learn
- Children prop
- Wrapping components
- Passing JSX as props
- Lifting state up
- Thinking in component trees
Read this lesson in your language
All lessons in this course
17 lessons- 1What is React?25 min
- 2JSX and Components30 min
- 3Props30 min
- 4State with useState35 min
- 5Handling Events30 min
- 6Conditional Rendering and Lists30 min
- 7Forms and Controlled Inputs35 min
- 8Effects with useEffect40 min
- 9Data Fetching40 min
- 10Component Composition35 min
- 11Context and useContext35 min
- 12useReducer35 min
- 13Custom Hooks35 min
- 14Performance and Memoization35 min
- 15Routing with React Router40 min
- 16Testing React Components40 min
- 17Capstone: Todo App60 min
