Children์ ์ฌ์ฉํด์ children prop๋ก ๋ฐ์ JSX๋ฅผ ์กฐ์ํ๊ณ ๋ณํํ ์ ์์ต๋๋ค.
const mappedChildren = Children.map(children, child =>
<div className="Row">
{child}
</div>
);๋ ํผ๋ฐ์ค
Children.count(children)
Children.count(children)๋ children ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ์์ ์์ ์๋ฅผ ๋ฐํํฉ๋๋ค.
import { Children } from 'react';
function RowList({ children }) {
return (
<>
<h1>Total rows: {Children.count(children)}</h1>
...
</>
);
}ํ๋ผ๋ฏธํฐ
children: ์ปดํฌ๋ํธ์์ ๋ฐ์childrenprop์ ๊ฐ.
๋ฐํ๊ฐ
children ๋ด๋ถ ๋
ธ๋์ ์.
์ฃผ์ ์ฌํญ
- ๋น ๋
ธ๋(
null,undefinedํน์ Boolean), ๋ฌธ์์ด, ์ซ์, React ์๋ฆฌ๋จผํธ๋ ๊ฐ๋ณ ๋ ธ๋๋ก ๊ฐ์ฃผํฉ๋๋ค. ๋ฐฐ์ด ์์ฒด๋ ๊ฐ๋ณ ๋ ธ๋๊ฐ ์๋์ง๋ง ๋ฐฐ์ด์ ์์ ์์๋ ๊ฐ๋ณ ๋ ธ๋๋ก ๊ฐ์ฃผํฉ๋๋ค. React ์๋ฆฌ๋จผํธ์ ํ์ ์์๋ ์ํํ์ง ์์ต๋๋ค. React ์๋ฆฌ๋จผํธ๋ ๋ ๋๋ง ๋์ง ์์ผ๋ฉฐ ์์ ์์๋ฅผ ์ํํ์ง ์์ต๋๋ค. Fragments ์ญ์ ์ํํ์ง ์์ต๋๋ค.
Children.forEach(children, fn, thisArg?)
Children.forEach(children, fn, thisArg?)๋ children ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๋ชจ๋ ์์ ์์์ ๋ํด ํน์ ์ฝ๋๋ฅผ ์คํํฉ๋๋ค.
import { Children } from 'react';
function SeparatorList({ children }) {
const result = [];
Children.forEach(children, (child, index) => {
result.push(child);
result.push(<hr key={index} />);
});
// ...ํ๋ผ๋ฏธํฐ
children: ์ปดํฌ๋ํธ์์ ๋ฐ์childrenprop์ ๊ฐ.fn: ๋ฐฐ์ด์forEach๋ฉ์๋ ์ฝ๋ฐฑ์ฒ๋ผ ๊ฐ ์์ ์์์์ ์คํํ ํจ์. ์์ ์์๋ฅผ ์ฒซ ๋ฒ์งธ ์ธ์๋ก, ์ธ๋ฑ์ค๋ฅผ ๋ ๋ฒ์งธ ์ธ์๋ก ๋ฐ์ต๋๋ค. ์ธ๋ฑ์ค๋ 0์์ ์์ํด์ ํธ์ถํ ๋๋ง๋ค ์ฆ๊ฐํฉ๋๋ค.- optional
thisArg:fnํจ์๊ฐ ํธ์ถ๋ ๋ ์ฌ์ฉ๋this์ ๊ฐ. ์๋ต ์undefined๋ก ๊ฐ์ฃผํฉ๋๋ค.
๋ฐํ๊ฐ
Children.forEach๋ undefined๋ฅผ ๋ฐํํฉ๋๋ค.
์ฃผ์ ์ฌํญ
- ๋น ๋
ธ๋(
null,undefinedํน์ Boolean), ๋ฌธ์์ด, ์ซ์, React ์๋ฆฌ๋จผํธ๋ ๊ฐ๋ณ ๋ ธ๋๋ก ๊ฐ์ฃผํฉ๋๋ค. ๋ฐฐ์ด ์์ฒด๋ ๊ฐ๋ณ ๋ ธ๋๊ฐ ์๋์ง๋ง ๋ฐฐ์ด์ ์์ ์์๋ ๊ฐ๋ณ ๋ ธ๋๋ก ๊ฐ์ฃผํฉ๋๋ค. React ์๋ฆฌ๋จผํธ์ ํ์ ์์๋ ์ํํ์ง ์์ต๋๋ค. React ์๋ฆฌ๋จผํธ๋ ๋ ๋๋ง ๋์ง ์์ผ๋ฉฐ ์์ ์์๋ฅผ ์ํํ์ง ์์ต๋๋ค. Fragments ์ญ์ ์ํํ์ง ์์ต๋๋ค.
Children.map(children, fn, thisArg?)
Children.map(children, fn, thisArg?)์ children ๋ฐ์ดํฐ ๊ตฌ์กฐ์์ ๊ฐ ์์ ์์๋ฅผ ๋งคํํ๊ฑฐ๋ ๋ณํํฉ๋๋ค.
import { Children } from 'react';
function RowList({ children }) {
return (
<div className="RowList">
{Children.map(children, child =>
<div className="Row">
{child}
</div>
)}
</div>
);
}ํ๋ผ๋ฏธํฐ
children: ์ปดํฌ๋ํธ์์ ๋ฐ์childrenprop์ ๊ฐ.fn: ๋ฒ ์ด์map๋ฉ์๋ ์ฝ๋ฐฑ๊ฐ์ ๋งคํ ํจ์. ์์ ์์๋ฅผ ์ฒซ ๋ฒ์งธ ์ธ์๋ก, ์ธ๋ฑ์ค๋ฅผ ๋ ๋ฒ์งธ ์ธ์๋ก ๋ฐ์ต๋๋ค. ์ธ๋ฑ์ค๋ 0์์ ์์ํด์ ํธ์ถํ ๋๋ง๋ค ์ฆ๊ฐํฉ๋๋ค. ํจ์๋ ๋น ๋ ธ๋(null,undefinedํน์ Boolean), ๋ฌธ์์ด, ์ซ์, React ์๋ฆฌ๋จผํธ ํน์ ๋ค๋ฅธ React ๋ ธ๋์ ๋ฐฐ์ด๊ณผ ๊ฐ์ React ๋ ธ๋๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.- optional
thisArg:fnํจ์๊ฐ ํธ์ถ๋ ๋ ์ฌ์ฉ๋this์ ๊ฐ. ์๋ต์undefined๋ก ๊ฐ์ฃผํฉ๋๋ค.
๋ฐํ๊ฐ
children์ด null์ด๊ฑฐ๋ undefined์ผ ๋ ํด๋น ๊ฐ์ ๋ฐํํฉ๋๋ค.
๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ fn ํจ์์์ ๋ฐํํ ๋
ธ๋๋ค๋ก ๊ตฌ์ฑ๋ ํ๋ฉด ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. ๋ฐํ๋ ๋ฐฐ์ด์ null๊ณผ undefined๋ฅผ ์ ์ธํ๊ณ ๋ฐํ๋ ๋
ธ๋๋ฅผ ๋ชจ๋ ํฌํจํฉ๋๋ค.
์ฃผ์ ์ฌํญ
-
๋น ๋ ธ๋(
null,undefinedํน์ Boolean), ๋ฌธ์์ด, ์ซ์, React ์๋ฆฌ๋จผํธ๋ ๊ฐ๋ณ ๋ ธ๋๋ก ๊ฐ์ฃผํฉ๋๋ค. ๋ฐฐ์ด ์์ฒด๋ ๊ฐ๋ณ ๋ ธ๋๊ฐ ์๋์ง๋ง ๋ฐฐ์ด์ ์์ ์์๋ ๊ฐ๋ณ ๋ ธ๋๋ก ๊ฐ์ฃผํฉ๋๋ค. React ์๋ฆฌ๋จผํธ์ ํ์ ์์๋ ์ํํ์ง ์์ต๋๋ค. React ์๋ฆฌ๋จผํธ๋ ๋ ๋๋ง ๋์ง ์์ผ๋ฉฐ ์์ ์์๋ฅผ ์ํํ์ง ์์ต๋๋ค. Fragments ์ญ์ ์ํํ์ง ์์ต๋๋ค. -
fn์์ key๋ฅผ ๊ฐ์ง ์๋ฆฌ๋จผํธ(ํน์ ์๋ฆฌ๋จผํธ์ ๋ฐฐ์ด)์ ๋ฐํํ๋ ๊ฒฝ์ฐ, ๋ฐํ๋ ์๋ฆฌ๋จผํธ์ key๋children์ ์๋ณธ ํญ๋ชฉ์ key์ ์๋์ผ๋ก ๊ฒฐํฉ๋ฉ๋๋ค.fn์์ ๋ฐฐ์ด๋ก ์ฌ๋ฌ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ, ๊ฐ ์๋ฆฌ๋จผํธ์ key๋ ์๋ก ๊ฐ์๋ง ๊ณ ์ ํ๋ฉด ๋ฉ๋๋ค.
Children.only(children)
Children.only(children)์ children์ด ๋จ์ผ React ์๋ฆฌ๋จผํธ์ธ์ง ํ์ธํฉ๋๋ค.
function Box({ children }) {
const element = Children.only(children);
// ...ํ๋ผ๋ฏธํฐ
children: ์ปดํฌ๋ํธ์์ ๋ฐ์childrenprop์ ๊ฐ.
๋ฐํ๊ฐ
children์ด ์ ํจํ ์๋ฆฌ๋จผํธ๋ผ๋ฉด ๊ทธ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค.
๊ทธ๋ ์ง ์๋ค๋ฉด, ์๋ฌ๋ฅผ throwํฉ๋๋ค.
์ฃผ์ ์ฌํญ
- ์ด ๋ฉ์๋๋
children์ผ๋ก ๋ฐฐ์ด (์๋ฅผ ๋ค์ดChildren.map์ ๋ฐํ ๊ฐ)์ ๋๊ธฐ๋ฉด ํญ์ ์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค. ๋ค์ ๋งํดchildren์ ๋จ์ผ ์๋ฆฌ๋จผํธ์ ๋ฐฐ์ด์ด ์๋๋ผ ๋จ์ผ React ์๋ฆฌ๋จผํธ์ฌ์ผ ํฉ๋๋ค.
Children.toArray(children)
Children.toArray(children)์ children ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก๋ถํฐ ๋ฐฐ์ด์ ์์ฑํฉ๋๋ค.
import { Children } from 'react';
export default function ReversedList({ children }) {
const result = Children.toArray(children);
result.reverse();
// ...ํ๋ผ๋ฏธํฐ
children: ์ปดํฌ๋ํธ์์ ๋ฐ์childrenprop์ ๊ฐ.
๋ฐํ๊ฐ
children์ ์ํ ์๋ฆฌ๋จผํธ๋ฅผ ํ๋ฉด ๋ฐฐ์ด๋ก ๋ฐํํฉ๋๋ค.
์ฃผ์ ์ฌํญ
- ๋น ๋
ธ๋(
null,undefined, ํน์ Booleans)๋ ๋ฐํ๋ ๋ฐฐ์ด์์ ์๋ต๋ฉ๋๋ค. ๋ฐํ๋ ์๋ฆฌ๋จผํธ์ key๋ ๊ธฐ์กด ์๋ฆฌ๋จผํธ์ key, ์ค์ฒฉ ์์ค๊ณผ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐ๋๋ฏ๋ก ๋ฐฐ์ด์ ํ๋ฉดํํ๋๋ผ๋ ๋์์ด ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
์ฌ์ฉ๋ฒ
children ๋ณํํ๊ธฐ
Children.map์ children prop๋ก ๋ฐ์ JSX๋ฅผ ๋ณํํฉ๋๋ค.
import { Children } from 'react';
function RowList({ children }) {
return (
<div className="RowList">
{Children.map(children, child =>
<div className="Row">
{child}
</div>
)}
</div>
);
}์ ์์์์ RowList๋ ๋ชจ๋ ์์ ์์๋ฅผ <div className="Row">๋ก ๊ฐ์ธ์ค๋๋ค. ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ RowList์ ์ธ ๊ฐ์ <p> ํ๊ทธ๋ฅผ children prop๋ก ๋๊ฒจ์ค๋ค๊ณ ๊ฐ์ ํด ๋ด
์๋ค.
<RowList>
<p>This is the first item.</p>
<p>This is the second item.</p>
<p>This is the third item.</p>
</RowList>์์ ๋์จ RowList ๊ตฌํ์ ํตํด ๋ ๋๋ง ๋ ์ต์ข
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
<div className="RowList">
<div className="Row">
<p>This is the first item.</p>
</div>
<div className="Row">
<p>This is the second item.</p>
</div>
<div className="Row">
<p>This is the third item.</p>
</div>
</div>Children.map์ map()์ ์ฌ์ฉํด ๋ฐฐ์ด์ ๋ณํํ๋ ๊ฒ๊ณผ ์ ์ฌํ์ง๋ง, children ๋ฐ์ดํฐ ๊ตฌ์กฐ๊ฐ ๋ถ๋ถ๋ช
ํ๊ฒ ์ทจ๊ธ๋๋ค๋ ์ฐจ์ด๊ฐ ์์ต๋๋ค. ์ฆ, ๋ฐฐ์ด์ผ ์ ์๋ค๊ณ ํ๋๋ผ๋ ํญ์ ๋ฐฐ์ด์ด๊ฑฐ๋ ๋ค๋ฅธ ํน์ ํ ๋ฐ์ดํฐ ํ์
์ผ ๊ฒ์ด๋ผ๊ณ ๊ฐ์ ํด์๋ ์ ๋ฉ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ children์ ๋ณํํ ๋๋ Children.map์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
import { Children } from 'react'; export default function RowList({ children }) { return ( <div className="RowList"> {Children.map(children, child => <div className="Row"> {child} </div> )} </div> ); }
Deep Dive
React์์ children prop๋ ๋ถ๋ถ๋ช
ํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก ์ทจ๊ธ๋ฉ๋๋ค. children์ด ๊ตฌ์กฐํ๋ ๋ฐฉ์์ ์์กดํ ์ ์๋ค๋ ์๋ฏธ์
๋๋ค. ๋ณํํ๊ฑฐ๋ ํํฐ๋งํ๊ฑฐ๋ ๊ฐ์๋ฅผ ์ธ๊ธฐ ์ํด์๋ Children ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
์ค์ ๋ก children ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ ๋ด๋ถ์ ์ผ๋ก ๋ฐฐ์ด๋ก ํํ๋๊ณ ๋ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ง์ฝ ํ๋์ ์์๋ง ์๋ค๋ฉด React๋ ๋ถํ์ํ ๋ฉ๋ชจ๋ฆฌ ์ค๋ฒํค๋๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ฐฐ์ด์ ์ถ๊ฐ๋ก ์์ฑํ์ง ์์ต๋๋ค. children prop์ ์ง์ ์ ๊ทผํ์ง ์๊ณ Children ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, ์ค์ ๋ก React๊ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ด๋ป๊ฒ ๊ตฌํํ๋๋ผ๋ ์ฝ๋๋ ๊นจ์ง์ง ์์ต๋๋ค.
children์ด ๋ฐฐ์ด์ด๋๋ผ๋ Children.map์ ์ ์ฉํ๊ฒ ์ธ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด Children.map์ ๋ฐํ๋ ์๋ฆฌ๋จผํธ์ key๋ฅผ ์ ๋ฌ๋ฐ์ children์ key์ ๋ณํฉํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ ์์์ฒ๋ผ ๊ฐ์ธ์ง๋๋ผ๋ ์๋ณธ JSX ์์ ์์๋ key๋ฅผ ์์ด๋ฒ๋ฆฌ์ง ์์ต๋๋ค.
๊ฐ ์์ ์์์์ ์ฝ๋ ์คํํ๊ธฐ
Children.forEach๋ children ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ๊ฐ ์์ ์์๋ฅผ ๋ฐ๋ณตํฉ๋๋ค. ๋ฐํ๋๋ ๊ฐ์ ์๊ณ ๋ฐฐ์ด์ forEach ๋ฉ์๋์ ์ ์ฌํฉ๋๋ค. ์์ฒด ๋ฐฐ์ด์ ๊ตฌ์ฑํ๋ ๋ฑ ์ปค์คํ
๋ก์ง์ ์คํํ ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
import { Children } from 'react'; export default function SeparatorList({ children }) { const result = []; Children.forEach(children, (child, index) => { result.push(child); result.push(<hr key={index} />); }); result.pop(); // Remove the last separator return result; }
import { Children } from 'react'; export default function RowList({ children }) { return ( <div className="RowList"> <h1 className="RowListHeader"> Total rows: {Children.count(children)} </h1> {Children.map(children, child => <div className="Row"> {child} </div> )} </div> ); }
children ๋ฐฐ์ด๋ก ๋ณํฉํ๊ธฐ
Children.toArray(children)๋ children ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ผ๋ฐ์ ์ธ JavaScript ๋ฐฐ์ด๋ก ๋ณ๊ฒฝํฉ๋๋ค. ์ด๊ฒ์ ์ฌ์ฉํด์ filter, sort, reverse์ ๊ฐ์ ๋ฐฐ์ด์ ๋ด์ฅ ๋ฉ์๋๋ฅผ ์กฐ์ํ ์ ์์ต๋๋ค.
import { Children } from 'react'; export default function ReversedList({ children }) { const result = Children.toArray(children); result.reverse(); return result; }
๋์
์ฌ๋ฌ ์ปดํฌ๋ํธ ๋ ธ์ถํ๊ธฐ
Children ๋ฉ์๋๋ก ์์ ์์๋ฅผ ์กฐ์ํ๋ ์ฝ๋๋ ์ทจ์ฝํ ์ ์์ต๋๋ค. JSX์์ ์ปดํฌ๋ํธ์ ์์ ์์๋ฅผ ์ ๋ฌํ ๋ ํด๋น ์ปดํฌ๋ํธ๊ฐ ๊ฐ๋ณ ์์ ์์๋ฅผ ์กฐ์ํ๊ฑฐ๋ ๋ณํ๋ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
๊ฐ๋ฅํ ํ Children ๋ฉ์๋๋ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค. ์๋ฅผ ๋ค์ด RowList์ ๊ฐ ์์ ์์๋ฅผ <div className="Row">๋ก ๊ฐ์ธ๋ ค๋ฉด, Row ์ปดํฌ๋ํธ๋ฅผ ๋ด๋ณด๋ด๊ณ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ row๋ฅผ ์ง์ ๊ฐ์ธ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
import { RowList, Row } from './RowList.js'; export default function App() { return ( <RowList> <Row> <p>This is the first item.</p> </Row> <Row> <p>This is the second item.</p> </Row> <Row> <p>This is the third item.</p> </Row> </RowList> ); }
Children.map๊ณผ ๋ฌ๋ฆฌ ์ด ๋ฐฉ์์ ๋ชจ๋ ์์ ์์๋ฅผ ์๋์ผ๋ก ๊ฐ์ธ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ Children.map์ ์์ ์์์ ๋ฌ๋ฆฌ ๋ ๋ง์ ์ปดํฌ๋ํธ๋ฅผ ์ถ์ถํด๋ ๋์ํ๋ค๋ ์ค์ํ ์ด์ ์ด ์์ต๋๋ค.
์๋ฅผ ๋ค์ด MoreRows ์ปดํฌ๋ํธ๋ฅผ ์ง์ ์ถ์ถํ๋๋ผ๋ ๋์ํฉ๋๋ค.
import { RowList, Row } from './RowList.js'; export default function App() { return ( <RowList> <Row> <p>This is the first item.</p> </Row> <MoreRows /> </RowList> ); } function MoreRows() { return ( <> <Row> <p>This is the second item.</p> </Row> <Row> <p>This is the third item.</p> </Row> </> ); }
<MoreRows />๊ฐ ๋จ์ผ ์์ ์์์ด์ ๋จ์ผ row๋ก ์ทจ๊ธ๋๊ธฐ ๋๋ฌธ์ Children.map์ ๋์ํ์ง ์์ต๋๋ค.
prop๋ก ๊ฐ์ฒด ๋ฐฐ์ด ๋ฐ๊ธฐ
prop๋ก ๋ช
์์ ์ผ๋ก ๋ฐฐ์ด์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ์๋ ์์์์ RowList๋ rows ๋ฐฐ์ด์ prop๋ก ๋ฐ์ต๋๋ค.
import { RowList, Row } from './RowList.js'; export default function App() { return ( <RowList rows={[ { id: 'first', content: <p>This is the first item.</p> }, { id: 'second', content: <p>This is the second item.</p> }, { id: 'third', content: <p>This is the third item.</p> } ]} /> ); }
rows๋ ์ผ๋ฐ์ ์ธ JavaScript ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์, RowList ์ปดํฌ๋ํธ๋ map๊ณผ ๊ฐ์ ๋ด์ฅ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด ํจํด์ ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ๋ฅผ ์์ ์์์ ํจ๊ป ์ ๋ฌํ ๋ ๋ ์ ์ฉํฉ๋๋ค. ์๋ ์์์์ TabSwitcher ์ปดํฌ๋ํธ๋ tabs prop๋ก ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ฐ์ต๋๋ค.
import TabSwitcher from './TabSwitcher.js'; export default function App() { return ( <TabSwitcher tabs={[ { id: 'first', header: 'First', content: <p>This is the first item.</p> }, { id: 'second', header: 'Second', content: <p>This is the second item.</p> }, { id: 'third', header: 'Third', content: <p>This is the third item.</p> } ]} /> ); }
JSX๋ก ์์ ์์๋ฅผ ์ ๋ฌํ ๋์ ๋ฌ๋ฆฌ ์ด๋ฐ ๋ฐฉ์์ ๊ฐ ์์ดํ
์ header์ ๊ฐ์ ์ถ๊ฐ ์ ๋ณด๋ฅผ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค. tabs๊ฐ ๋ฐฐ์ด ๊ตฌ์กฐ์ด๊ณ ์ง์ ๋ค๋ค์ง๊ธฐ ๋๋ฌธ์ Children ๋ฉ์๋๋ ํ์ํ์ง ์์ต๋๋ค.
render prop๋ก ๋ ๋๋ง ์ปค์คํ ํ๊ธฐ
๋ชจ๋ ๊ฐ๋ณ ํญ๋ชฉ์ ๋ํด JSX๋ฅผ ์์ฑํ๋ ๋์ JSX๋ฅผ ๋ฐํํ๋ ํจ์๋ฅผ ์ ๋ฌํ๊ณ ํ์ํ ๋ ํด๋น ํจ์๋ฅผ ํธ์ถํ ์๋ ์์ต๋๋ค. ์๋ ์์์์ App ์ปดํฌ๋ํธ๋ renderContent ํจ์๋ฅผ TabSwitcher ์ปดํฌ๋ํธ์ ์ ๋ฌํฉ๋๋ค. TabSwitcher ์ปดํฌ๋ํธ๋ ์ ํ๋ ํญ์ ๋ํด์๋ง renderContent๋ฅผ ํธ์ถํฉ๋๋ค.
import TabSwitcher from './TabSwitcher.js'; export default function App() { return ( <TabSwitcher tabIds={['first', 'second', 'third']} getHeader={tabId => { return tabId[0].toUpperCase() + tabId.slice(1); }} renderContent={tabId => { return <p>This is the {tabId} item.</p>; }} /> ); }
renderContent์ ๊ฐ์ด ์ฌ์ฉ์ ์ธํฐํ์ด์ค์ ์ผ๋ถ๋ฅผ ์ด๋ป๊ฒ ๋ ๋๋งํ ์ง ์ ์ํ๋ prop๋ฅผ render prop๋ผ๊ณ ํฉ๋๋ค. ํ์ง๋ง ํน๋ณํ ๊ฒ์ ์๋๋๋ค. ๋จ์ง ์ผ๋ฐ์ ์ธ ํจ์์ prop์ผ ๋ฟ์
๋๋ค.
Render props๋ ํจ์์ด๋ฏ๋ก ์ ๋ณด๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค.
์๋ ์์์์ RowList ์ปดํฌ๋ํธ๋ ๊ฐ row์ id์ index๋ฅผ renderRow์ render prop๋ก ์ ๋ฌํ๊ณ , index๊ฐ ์ง์์ธ row๋ฅผ ๊ฐ์กฐํฉ๋๋ค.
import { RowList, Row } from './RowList.js'; export default function App() { return ( <RowList rowIds={['first', 'second', 'third']} renderRow={(id, index) => { return ( <Row isHighlighted={index % 2 === 0}> <p>This is the {id} item.</p> </Row> ); }} /> ); }
๋ถ๋ชจ ์ปดํฌ๋ํธ์ ์์ ์ปดํฌ๋ํธ๊ฐ ์์ ์์๋ฅผ ์ง์ ์กฐ์ํ์ง ์๊ณ ๋ ํจ๊ณผ์ ์ผ๋ก ํ๋ ฅํ ์ ์๋ ์ข์ ์์์ ๋๋ค.
๋ฌธ์ ํด๊ฒฐ
์ปค์คํ
์ปดํฌ๋ํธ๋ฅผ ์ ๋ฌํ์ ๋ Children ๋ฉ์๋๊ฐ ๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ์ง ์๋ ๊ฒฝ์ฐ
RowList์ ๋ ๊ฐ์ ์์ ์์๋ฅผ ์๋์ ๊ฐ์ด ์ ๋ฌํ๋ค๊ณ ๊ฐ์ ํด ๋ด
์๋ค.
<RowList>
<p>First item</p>
<MoreRows />
</RowList>RowList ๋ด๋ถ์์ Children.count(children)๋ฅผ ์คํ์ํจ๋ค๋ฉด ๊ฒฐ๊ณผ๋ 2์
๋๋ค. MoreRows๊ฐ 10๊ฐ์ ๋ค๋ฅธ ์์๋ฅผ ๋ ๋๋งํ๊ฑฐ๋ null์ ๋ฐํํ๋๋ผ๋, Children.count(children)๋ 2์
๋๋ค. RowList ๊ด์ ์์๋ ๊ทธ๊ฒ์ด ๋ฐ์ JSX๋ง ๊ณ ๋ คํ ๋ฟ MoreRows ์ปดํฌ๋ํธ์ ๋ด๋ถ๋ ๊ณ ๋ คํ์ง ์์ต๋๋ค.
์ด๋ฐ ์ ์ฝ๋๋ฌธ์ ์ปดํฌ๋ํธ๋ฅผ ์ถ์ถํ๊ธฐ ์ด๋ ค์ธ ์ ์์ต๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Children ๋์ ๋์์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.