React · Junior · Question 7 of 75
How do you return two sibling elements from a component without an extra DOM wrapper?
- 1Return an array without keys:
return [<A />, <B />] - ✓Correct answer: Wrap them in a fragment:
<>...</>or<Fragment> - 3It's impossible - a component always adds a
<div>
Explanation
A component must return a single root. A fragment groups elements without adding a DOM node. If the fragment needs a key (e.g. in a list), use the full form <Fragment key={id}>. An array is also allowed, but each element needs a key, otherwise you get a warning.