easyFrontend

What are fragments in React?

485 views
01

Understand the problem

A simple yet useful feature for cleaner component structure.

fragmentsdomjsxrendering
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Return siblings without a wrapper
import { Fragment } from "react";

function Columns() {
  return (
    <>                          {/* shorthand fragment — no extra <div> */}
      <td>Name</td>
      <td>Role</td>
    </>
  );
}

export default function App() {
  const rows = [{ id: 1, a: "Ada", b: "Eng" }];
  return (
    <table style={{ fontFamily: "system-ui", margin: 24 }}>
      <tbody>
        <tr><Columns /></tr>
        {rows.map((r) => (
          <Fragment key={r.id}>   {/* keyed fragment in a list */}
            <tr><td>{r.a}</td><td>{r.b}</td></tr>
          </Fragment>
        ))}
      </tbody>
    </table>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.