Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

React Component Composition Patterns: Props, Children, State, Context, and Hooks

Choose React composition patterns by what you need to share: JSX through children or named props, coordinated values through lifted state, deep data through selective context, and reusable behavior through custom Hooks.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

React components compose most clearly when each shared thing uses the right channel: pass markup through children or a named JSX prop, pass ordinary data through props, lift a coordinated state value to the closest common parent, use context when many or distant descendants need the same information, and extract custom Hooks to reuse stateful logic. These approaches solve different problems; none is a universal substitute for the others.

Use props and JSX to compose content

Props are the normal way for a parent component to communicate with a child. They can carry strings, objects, functions, and JSX—not just simple values. A component can therefore provide a wrapper while leaving the choice of inner content to its caller.

function Panel({ children }) {
  return <section className="panel">{children}</section>;
}

function AccountPage() {
  return (
    <Panel>
      <h2>Account</h2>
      <p>Manage your profile settings.</p>
    </Panel>
  );
}

JSX nested between a component’s opening and closing tags is supplied to that component as its children prop. This creates a flexible content slot: Panel handles the surrounding structure without needing to know what its caller places inside. React documents this use of nested content in Passing Props to a Component and describes children as a React node supplied through JSX nesting in Common components (e.g. <div>).

When a component has several distinct content areas, a named prop can make the API clearer than multiple positional or nested conventions:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function Dialog({ title, actions, children }) {
  return (
    <section role="dialog">
      <header>{title}</header>
      <div>{children}</div>
      <footer>{actions}</footer>
    </section>
  );
}

<Dialog
  title={<h2>Delete item?</h2>}
  actions={<button>Cancel</button>}
>
  <p>This cannot be undone.</p>
</Dialog>

Use children when the content naturally belongs inside a wrapper. Use a named JSX prop when the component’s API has a specific slot—such as a title, icon, or action area—that benefits from an explicit name.

Keep data flow explicit with props

Props make dependencies visible: the parent supplies a value, and the child receives it for rendering or for a callback it can invoke. Treat props as read-only snapshots for a render. A child should not change a prop to update its parent; the parent owns the value and can pass a new one on a later render. React explains this model in Passing Props to a Component.

Passing props through a component that does not use them can become repetitive. Before adding broad prop spreads or forwarding several values through layers, check whether an intermediate component can instead receive the relevant JSX as children. That lets a layout arrange content without knowing every data dependency of the content it hosts. Explicit props remain useful when each intermediate component genuinely needs to inspect or transform the value.

Coordinate components by lifting state

If two or more components must stay in sync around one value, put that state in their closest common parent. The parent passes the current value down and supplies handlers through which children can request changes. This is called lifting state up; React’s guide Sharing State Between Components uses coordinated accordion panels to illustrate the approach.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function Accordion() {
  const [activeIndex, setActiveIndex] = useState(0);

  return (
    <>
      <Panel
        isActive={activeIndex === 0}
        onShow={() => setActiveIndex(0)}
      />
      <Panel
        isActive={activeIndex === 1}
        onShow={() => setActiveIndex(1)}
      />
    </>
  );
}

function Panel({ isActive, onShow }) {
  return isActive
    ? <p>Panel content</p>
    : <button onClick={onShow}>Show</button>;
}

Here the parent owns the single active-panel value. Each child receives a derived Boolean and a callback; the child does not maintain a competing copy of the same state. A component is controlled when its important value is supplied and managed by its parent. It is uncontrolled when it manages that value internally. Choose based on who needs to coordinate the value, not on a blanket preference for one style.

Use context for information needed deeply or broadly

Prop drilling is the repeated passage of a value through components that do not themselves use it, simply so a deeper descendant can receive it. Context makes a value available to descendants without every intermediate component forwarding it. React’s Passing Data Deeply with Context discusses uses such as theming, the current account, routing, and state management.

Context trades explicit local dependencies for access across a descendant tree. Before introducing it, consider whether direct props are still clear, or whether component extraction can let an intermediate layout accept already-composed children. React specifically recommends considering these alternatives because context is easy to overuse. Prefer props when they make it obvious which component consumes a value; consider context when many or distant descendants genuinely need shared information.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use custom Hooks to reuse behavior, not one shared state instance

A custom Hook packages reusable stateful logic behind a function whose name starts with use. For example, separate components can call the same online-status Hook to reuse how they subscribe to status changes. The Hook avoids duplicating that behavior, but each call has its own state. Calling a Hook in two components does not make those components share one state value. React makes this distinction in Reusing Logic with Custom Hooks.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If components need one coordinated value, lift that state to a common owner and pass it down, or consider context when the value is broadly needed. A custom Hook can still help package the logic for that owner, but it is not itself a shared-state channel.

Keep composition correct during rendering

  • Keep components and Hooks pure. Given the same inputs, rendering should produce the same result; do not perform side effects as part of render.
  • Do not mutate props or state. Treat them as immutable and request updates through React’s state mechanisms.
  • Let React invoke components. Render a component with JSX, such as <Panel />, rather than calling Panel() as an ordinary function.

These rules preserve React’s ability to manage rendering consistently. See Rules of React.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.