Sub topic 1
Composition and Other React Component Patterns
Prerequisites
Assume you know function components, JSX, props, useState, event handlers, and ES module imports.
Composition and the children prop
Composition means building a component from smaller components instead of adding a prop for every possible layout. The children prop contains JSX placed between a component's opening and closing tags.
function Card({ title, children }) {
return (
<section>
<h2>{title}</h2>
{children}
</section>
);
}
export function App() {
return (
<Card title="Account">
<p>Signed in as Ada.</p>
<button>Sign out</button>
</Card>
);
}Card controls the container and title. The caller controls the content and its order. This keeps the component flexible without a large configuration API.
Lifting state up and its limits
When two sibling components need the same state, move that state to their nearest common parent. This is called lifting state up. The parent passes the current value and event handlers to each child.
This approach makes data flow explicit, but it can create prop drilling. Prop drilling means passing props through components that do not use them. It also couples the parent to the exact structure of its children.
Render props
A render prop is a prop whose value is a function. The component owns reusable behavior and calls the function with state or handlers. The caller chooses the rendered markup.
function Toggle({ children }) {
const [on, setOn] = React.useState(false);
return children({ on, toggle: () => setOn((value) => !value) });
}
export function App() {
return (
<Toggle>
{({ on, toggle }) => (
<button onClick={toggle}>{on ? 'On' : 'Off'}</button>
)}
</Toggle>
);
}In a module, import React or import useState and call it directly. Render props are flexible, but deeply nested functions can reduce readability.
Higher-order components
A higher-order component, or HOC, is a function that accepts a component and returns an enhanced component. HOCs can add data, permissions, or behavior without changing the original component. They can also make debugging and prop flow harder, especially when several HOCs wrap one component.
Custom hooks for reusable logic
A custom hook is a function whose name starts with use and that calls React hooks. It shares stateful logic without sharing markup. Prefer a custom hook when multiple components need the same behavior but different UI.
Sharing state with context
Context lets a parent provide a value to deeply nested descendants without passing props through every layer. A consumer reads the value with useContext. Use it for a specific shared concern, such as the current user, theme, or tab state. Keep the context value focused because changes can re-render all consumers.
Exercise
Which pattern should you choose when components need shared behavior but different markup?
Show answer
Start with a custom hook. Use context when the state must reach deeply nested descendants, and use a render prop when the caller benefits from receiving behavior directly.