Engrain by ByteRush

RCT.08.01 Mid-level· 10 minCore — asked often

Composition, children and slots: fewer props, more flexible components

Prefer composition over configuration: pass children and named slot props instead of dozens of flags, avoid prop drilling by passing elements down, and see how hooks changed the container/presentational split.

Comes up in: React, Low-level design, Machine coding

Learn#

Megha’s Card component has 22 props: title, subtitle, showIcon, iconPosition, footerAlign, hideFooter and more. Every new design needs a new prop, and nobody can remember them all.

Composition means building UI by putting components inside each other, instead of configuring one component with flags. children is the main slot. A slot is any prop that takes elements, such as header or actions, so callers decide what goes there:

import { createElement as h } from "react";
import { renderToStaticMarkup } from "react-dom/server";

const user = { name: "Asha" };
// Drilling: Page → Layout → Header → Avatar, and only Avatar uses user.
const Avatar = ({ user }) => h("img", { alt: user.name });
const HeaderDrilled = ({ user }) => h("header", null, h(Avatar, { user }));
const LayoutDrilled = ({ user, children }) => h("div", null, h(HeaderDrilled, { user }), children);

// Composition: Layout takes the header as a slot; nobody in between needs to know about user.
const Header = ({ children }) => h("header", null, children);
const Layout = ({ header, children }) => h("div", null, header, children);

const drilled = h(LayoutDrilled, { user }, h("main", null, "Orders"));
const composed = h(Layout, { header: h(Header, null, h(Avatar, { user })) }, h("main", null, "Orders"));
console.log(renderToStaticMarkup(drilled));
console.log(renderToStaticMarkup(composed) === renderToStaticMarkup(drilled) ? "same HTML, no drilling" : "different");
This example uses Node.js, so it runs in our checks rather than in your browser.

Output (verified)

<div><header><img alt="Asha"/></header><main>Orders</main></div>
same HTML, no drilling

Prop drilling is passing a prop through components that do not use it. Composition removes it: build the element where the data is, and pass the element down.

From drilling to a slotStep 1 of 4

State

  • Layout: user (passes on)
  • Header: user (passes on)
  • Avatar: user (uses)

Page

    empty

    Drilled: Page passes user to Layout, Layout passes it to Header, Header passes it to Avatar. Only Avatar uses it.

    Read the steps as text
    1. Drilled: Page passes user to Layout, Layout passes it to Header, Header passes it to Avatar. Only Avatar uses it.
    2. Adding a field means editing three components that never read it.
    3. Composed: Page builds <Avatar user={user}/> itself and hands it to Layout's header slot.
    4. Layout and Header just place what they are given. The same HTML comes out, with no drilling.

    Predict#

    Predict first

    Five levels of components pass theme down, and only the deepest uses it. What should you try first?

    Interview angle#

    Say it like this — about 30 seconds

    “I prefer composition over configuration: instead of adding props like showIcon or footerAlign, a component accepts children and named slots such as header or actions, and the caller decides what goes in. That also fixes most prop drilling, because the element is built where the data lives and passed down already rendered. If many distant components need the same value, such as theme or the current user, I use context. With hooks, the container and presentational split mostly became custom hooks plus simple components.”

    Stays on your device. Nothing is uploaded.
    Follow-up How do you avoid prop drilling?
    Composition first (children and slots), then context for widely needed values, then a store for app-wide changing state.
    Follow-up Does a slot prop re-render when the parent renders?
    The element is created by the parent, so it re-renders when the parent does, but middle layers that only place it do not cause extra renders.
    Follow-up What replaced container components?
    Custom hooks that hold data and logic, used by components that render.

    Practice#

    Guided

    Write a small analysis that finds prop drilling in a component tree.

    Done when

    • A prop received, unused and passed to a child is reported as “Node.prop”.
    • Props a node uses itself are not reported.
    • Results come in depth-first order.
    Your solution
    Ctrl/⌘ + Enter

    Remember#

    Try to answer each card before you open it. Then add them to your reviews.

    What is prop drilling?

    Passing a prop through components that do not use it.

    What is a slot prop?

    A prop that receives elements, such as header or actions.

    First fix for drilling?

    Composition: build the element where the data is and pass it down.

    What replaced most container components?

    Custom hooks.

    Go deeper

    Four layers. Stop at the one your interview needs; each ends with a quick check.

    The core ideaEvery round
    Each flag prop doubles the states a component can be in. Slots let callers add icons, badges or actions without the component knowing about them Source: React: Passing JSX as children.
    Check yourself: What is composition over configuration?

    Accept children and slots instead of many flag props.

    How it works underneathSenior rounds

    children is a normal prop; JSX nested inside a component is assigned to it. Any prop can hold elements, so a component can have several named slots.

    Check yourself: Are children a special mechanism?

    No. children is a normal prop holding elements.

    Edge cases and trapsWhere people slip

    Children created in the parent are new elements on every parent render, so memo on a component that takes children rarely helps (RCT.03.04). Slots also make it harder for a component to control its children’s props; that is the trade-off of flexibility.

    Check yourself: Why might a memo component with children still re-render?

    The children elements are new objects each parent render.

    In productionStaff-level answers
    The old container/presentational split is now a custom hook plus a simple component. Use context when many components at different depths need the same value Source: React: Before you use context.
    Check yourself: When is context better than composition?

    When many components at different depths need the same value.

    Finished the five steps? Mark it, and its cards join your reviews.

    Last checked 2026-11-01. Spot a mistake? Use “Report an error” below — fixes are listed on the corrections page.

    Interview questions this answers

    • How do you avoid prop drilling?