← React Beginner Questions - Set 6 Q55
React Beginner Questions - Set 6
A component returns one value; JSX with two roots is a syntax error, not a React rule. A fragment satisfies it without adding a DOM node.
// Syntax error: adjacent JSX elements must be wrapped
// return <h1>A</h1><p>B</p>;
// Valid, but adds a wrapper element to the DOM
return <div><h1>A</h1><p>B</p></div>;
// Valid and invisible in the DOM
return <><h1>A</h1><p>B</p></>;
// Arrays work too, and then each item needs a key
return [<h1 key="h">A</h1>, <p key="p">B</p>];