One Parent Rule
Part of the Fundamentals section of Coddy's React journey — lesson 3 of 42.
A component can return as much JSX as you like, with one rule: everything must live inside one parent element. This is fine:
return (
<div>
<h1>My Page</h1>
<p>Welcome!</p>
</div>
);But returning two elements side by side with no wrapper is an error. A function can only return one thing:
// BROKEN: two top-level elements
return (
<h1>My Page</h1>
<p>Welcome!</p>
);The usual fix is wrapping everything in a <div>. Watch the preview: if you break the rule, the build error tells you exactly where.
Challenge
BeginnerBuild a small page header: two elements under one parent.
- Keep the outer
<div>. - Inside it add an
<h1>withid="heading"and the textMy First App. - Below the heading add a
<p>withid="tagline"and the textBuilt with React.
Try it yourself
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script type="module" src="main.jsx"></script>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2JSX Expressions
Curly BracesMath in BracesBraces in AttributesCalling Functions in JSXRecap: JSX Expressions5State & Events
What Is State?Click CounterCount in StepsToggle with StateIndependent StateRecap: State & Events8Project: Profile Card
Step 1: Card StructureStep 2: Card with PropsStep 3: Like ButtonStep 4: Follow Toggle