Question presented to candidate: "A component needs to return two sibling elements. What are your options, and why would you reach for a Fragment?"
What a strong answer should cover:
- A component returns one value, so multiple siblings need a single parent — a Fragment is that parent without the DOM cost.
- A Fragment renders no DOM node at all; children land directly in the parent element.
- Two syntaxes: the shorthand
<>...</>and the explicit<Fragment>...</Fragment>. - The shorthand cannot take a
key. Mapping over a list that emits sibling pairs requires the long form. - Why the wrapper div actually matters: it breaks CSS layout contracts (flex/grid parent-child relationships), produces invalid HTML inside
<table>,<dl>,<ul>, and adds depth to the DOM. Fragmentis exported from bothreactandreact/jsx-runtime— the same symbol.- Nuance: Fragments accept only
keyandchildren; noclassName, nostyle, no ref.
Clarifying questions expected:
- "Is this inside a list where each item emits multiple siblings?" — that decides shorthand versus long form.
Code / implementation expected: Yes — a keyed Fragment inside a map is the case worth showing.