<Fragment>๋Š” ์ข…์ข… <>...</> ๊ตฌ๋ฌธ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ , ๋ž˜ํผ ๋…ธ๋“œ ์—†์ด ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<>
<OneChild />
<AnotherChild />
</>

Reference

<Fragment>

ํ•˜๋‚˜์˜ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ํ•„์š”ํ•œ ์ƒํ™ฉ์—์„œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ <Fragment>๋กœ ๊ฐ์‹ธ์„œ ๊ทธ๋ฃนํ™”ํ•˜์„ธ์š”. Fragment ์•ˆ์—์„œ ๊ทธ๋ฃนํ™”๋œ ์—˜๋ฆฌ๋จผํŠธ๋Š” DOM ๊ฒฐ๊ณผ๋ฌผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๊ทธ๋ฃนํ™”๋˜์ง€ ์•Š์€ ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋นˆ JSX ํƒœ๊ทธ์ธ <></>๋Š” <Fragment></Fragment>์˜ ์ค„์ž„๋ง์ž…๋‹ˆ๋‹ค.

Props

  • optional key: ๋ช…์‹œ์  <Fragment>๋กœ ์„ ์–ธ๋œ Fragment์—๋Š” key๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์˜ ์‚ฌํ•ญ

  • Fragment์— key๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด <>...</> ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ช…์‹œ์ ์œผ๋กœ react์—์„œ Fragment๋ฅผ importํ•˜๊ณ  <Fragment key={yourKey}>...</Fragment>๋ฅผ ๋ Œ๋”๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • React๋Š” <><Child /></>์—์„œ [<Child />]๋กœ ๋ Œ๋”๋งํ•˜๊ฑฐ๋‚˜ (๋˜๋Š” ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ), ํ˜น์€ <><Child /></> ์—์„œ <Child /> ๋ Œ๋”๋งํ•˜๊ฑฐ๋‚˜ (๋˜๋Š” ๋ฐ˜๋Œ€์˜ ๊ฒฝ์šฐ) state๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์˜ค์ง single level๊นŒ์ง€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด <><><Child /></></> ์—์„œ <Child />๋กœ ๋ Œ๋”๋งํ•˜๋Š” ๊ฒƒ์€ state๊ฐ€ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ semantics๋Š” ์—ฌ๊ธฐ์„œ ์ฐธ์กฐํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์‚ฌ์šฉ๋ฒ•

์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ ๋ฐ˜ํ™˜ํ•˜๊ธฐ

์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ ์œ„ํ•ด Fragment๋‚˜ <>...</> ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์„ธ์š”. ํ•œ ๊ฐœ์˜ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ณณ์— ์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ปดํฌ๋„ŒํŠธ๋Š” ํ•œ ๊ฐœ์˜ ์—˜๋ฆฌ๋จผํŠธ๋งŒ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ Fragment๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function Post() {
return (
<>
<PostTitle />
<PostBody />
</>
);
}

Fragment๋กœ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๋ฉด DOM ์—˜๋ฆฌ๋จผํŠธ์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ๋กœ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ์‹ธ๋Š” ๊ฒฝ์šฐ์™€๋Š” ๋‹ฌ๋ฆฌ ๋ ˆ์ด์•„์›ƒ์ด๋‚˜ ์Šคํƒ€์ผ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Fragment๋Š” ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋กœ ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๊ฒ€์‚ฌํ•˜๋ฉด ๋ชจ๋“  <h1>, <article> DOM ๋…ธ๋“œ๊ฐ€ ๋ž˜ํผ ์—†์ด ํ˜•์ œ ๋…ธ๋“œ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

export default function Blog() {
  return (
    <>
      <Post title="An update" body="It's been a while since I posted..." />
      <Post title="My new blog" body="I am starting a new blog!" />
    </>
  )
}

function Post({ title, body }) {
  return (
    <>
      <PostTitle title={title} />
      <PostBody body={body} />
    </>
  );
}

function PostTitle({ title }) {
  return <h1>{title}</h1>
}

function PostBody({ body }) {
  return (
    <article>
      <p>{body}</p>
    </article>
  );
}

Deep Dive

ํŠน๋ณ„ํ•œ ๋ฌธ๋ฒ• ์—†์ด Fragment๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์œ„์˜ ์˜ˆ์‹œ๋Š” React์—์„œ Fragment๋ฅผ import ํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

import { Fragment } from 'react';

function Post() {
return (
<Fragment>
<PostTitle />
<PostBody />
</Fragment>
);
}

์ผ๋ฐ˜์ ์œผ๋กœ Fragment์— key๋ฅผ ๋„˜๊ฒจ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ด ๊ธฐ๋Šฅ์€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


๋ณ€์ˆ˜์— ์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ ํ• ๋‹น

๋‹ค๋ฅธ ์—˜๋ฆฌ๋จผํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Fragment๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  props๋กœ ์ „๋‹ฌํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function CloseDialog() {
const buttons = (
<>
<OKButton />
<CancelButton />
</>
);
return (
<AlertDialog buttons={buttons}>
Are you sure you want to leave this page?
</AlertDialog>
);
}

ํ…์ŠคํŠธ์™€ ํ•จ๊ป˜ ์—˜๋ฆฌ๋จผํŠธ ๊ทธ๋ฃนํ™”

Fragment๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ปดํฌ๋„ŒํŠธ์™€ ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function DateRangePicker({ start, end }) {
return (
<>
From
<DatePicker date={start} />
to
<DatePicker date={end} />
</>
);
}

Fragment ๋ฆฌ์ŠคํŠธ ๋ Œ๋”๋ง

<></> ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ๋ช…์‹œ์ ์œผ๋กœ Fragment๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ณต์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ ๊ฐ ์š”์†Œ์— key๋ฅผ ํ• ๋‹นํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณต ์•ˆ์— ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ Fragment์ธ ๊ฒฝ์šฐ key ์†์„ฑ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ฐ˜ JSX ์—˜๋ฆฌ๋จผํŠธ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

function Blog() {
return posts.map(post =>
<Fragment key={post.id}>
<PostTitle title={post.title} />
<PostBody body={post.body} />
</Fragment>
);
}

DOM์„ ๊ฒ€์‚ฌํ•˜์—ฌ Fragment ์ž์‹ ์ฃผ์œ„์— ๋ž˜ํผ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import { Fragment } from 'react';

const posts = [
  { id: 1, title: 'An update', body: "It's been a while since I posted..." },
  { id: 2, title: 'My new blog', body: 'I am starting a new blog!' }
];

export default function Blog() {
  return posts.map(post =>
    <Fragment key={post.id}>
      <PostTitle title={post.title} />
      <PostBody body={post.body} />
    </Fragment>
  );
}

function PostTitle({ title }) {
  return <h1>{title}</h1>
}

function PostBody({ body }) {
  return (
    <article>
      <p>{body}</p>
    </article>
  );
}