Question presented to candidate: "React has no component inheritance. So how do you share and extend behaviour — and what does 'composition over inheritance' mean in practice here?"
What a strong answer should cover:
- Composition means building complex UI by combining components rather than extending them. React has no inheritance model at all, deliberately.
- The mechanisms:
childrenfor arbitrary content, props as slots (passing elements, not just data), render props, and custom Hooks for shared behaviour. - The distinction that matters: composition shares structure; custom Hooks share behaviour.
- Containment (a generic wrapper that does not know its content) versus specialisation (a specific component configuring a generic one).
- A measurable benefit: a child passed as
childrenis created by the grandparent, so its element is the same object across the parent's re-renders — React bails out of re-rendering it, with nomemoneeded. - This makes composition a legitimate performance technique, not just an organisational one.
- Why inheritance is avoided: it couples components to a hierarchy, and UI variation is rarely a clean single-axis taxonomy.
Clarifying questions expected:
- "Are we sharing markup/structure, or behaviour?" — that picks composition versus a custom Hook.
Code / implementation expected: Yes — a wrapper taking children, ideally showing the re-render benefit.