๐Ÿถ

React ์ตœ์ƒ์œ„ API

์ž‘์„ฑ์ž
์œ ์ง€์˜
๋ฐœํ‘œ์ผ
Dec 23, 2022
No
19

์—˜๋ฆฌ๋จผํŠธ ๋ณ€ํ™˜

  • cloneElement()
  • isValidElement()
  • React.Children
 
 

cloneElement()

React.cloneElement( element, [config], [...children] )
element๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒˆ๋กœ์šด React ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ณต์‚ฌํ•˜๊ณ  ๋ฐ˜ํ™˜.
config๋Š” key์™€ ref, ์ƒˆ๋กœ์šด props๋ฅผ ํฌํ•จํ•˜๋ฉฐ ์ƒˆ๋กœ์šด ์—˜๋ฆฌ๋จผํŠธ์—๋Š” ์›๋ณธ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๊ฐ€์กŒ๋˜ props์™€ ์ƒˆ๋กœ์šด props ๊ฐ€ ์–•๊ฒŒ ํ•ฉ์ณ์ง„๋‹ค.
 

isValidElement()

React.isValidElement(object)
๊ฐ์ฒด๊ฐ€ React ์—˜๋ฆฌ๋จผํŠธ์ธ์ง€ ํ™•์ธํ•ด์„œ true ํ˜น์€ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 

React.Children

:๋ถˆํˆฌ๋ช… ์ž๋ฃŒ๊ตฌ์กฐ์ธ this.props.children์„ ๋‹ค๋ฃจ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.
 
๋ถˆํˆฌ๋ช… ์ž๋ฃŒ๊ตฌ์กฐ๋ž€? ๊ตฌ์ฒด์ ์ธ ๋ฐ์ดํ„ฐ๊ตฌ์กฐ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
 
  • React.Children.map
React.Children.map(children, function[(thisArg)])
children์— ํฌํ•จ๋œ ์ž์‹์— ๋Œ€ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๋กœ ์ƒ์„ฑํ•œ๋‹ค.
children์ด null๋˜๋Š” undefined์ผ ๊ฒฝ์šฐ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ null ๋˜๋Š” undefined๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋‹ค.
 
๐Ÿ’ก
children์ด Fragment์ผ ๊ฒฝ์šฐ children์€ ๋‹จ์ผ ์ž์‹ ์ทจ๊ธ‰๋˜์–ด ์ˆœํšŒํ•˜์ง€ ์•Š๋Š”๋‹ค. (?)
 
  • React.children.forEach
React.Children.forEach(children, function[(thisArg)])
map๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.
 
  • React.Children.count
React.Children.count(children)
children์— ํฌํ•จ๋œ ์ปดํฌ๋„ŒํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
 
  • React.Children.only
React.Children.only(children)
children์ด ๋‹จ ํ•˜๋‚˜์˜ ์ž์‹์„ ๊ฐ–๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ํ•ด๋‹น ์ž์‹ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
 
๐Ÿ’ก
React.Children.only() ๋Š” React.Children.map() ์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ ์ด์œ ๋Š” map์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ React ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ!
 
  • React.Children.toArray
React.Children.toArray(children)
๊ฐ ์ž์‹์— ๊ณ ์œ ์˜ key๋ฅผ ํ• ๋‹นํ•ด์„œ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. children์„ ๋‹ค์‹œ ์ •๋ ฌํ•˜๊ฑฐ๋‚˜ ์ผ๋ถ€๋งŒ ์ž˜๋ผ๋‚ด๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•˜๋‹ค.
import React from "react"; export default function Sample({ children }) { console.log(children); console.log(React.Children.toArray(children)); return children; }
import Sample from "../components/sample/Sample"; export default { title: "components/Sample", component: Sample, }; const arr = [ { id: 1, fruits: "banana" }, { id: 2, fruits: "apple" }, { id: 3, fruits: "cherry" }, ]; export const Default = () => { return ( <Sample> <div fruits="mango">mango</div> {arr.map((value) => ( <li key={value.id}>{value.fruits}</li> ))} </Sample> ); };
notion image
 
 
children
children
React.Children.toArray(children)
React.Children.toArray(children)
 
toArray ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ฐ ์ž์‹์—๊ฒŒ ๊ณ ์œ ํ•œ key๋ฅผ ํ• ๋‹นํ•œ๋‹ค.
์ด๋Ÿฌํ•œ ๊ณ ์œ ํ•œ ํ‚ค๋Š” children์„ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค ๋•Œ ์ค‘์ฒฉ๋œ ๋ฐฐ์—ด์˜ ์˜๋ฏธ๋ฅผ ๋ณด์กดํ•˜๊ธฐ ์œ„ํ•œ ์กฐ์น˜์ด๋‹ค.
๋”ฐ๋ผ์„œ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์ธ arr๋Š” ๋˜‘๊ฐ™์ด keyrk .1๋กœ ์‹œ์ž‘ํ•œ๋‹ค!
 

React.Fragment

์ถ”๊ฐ€์ ์ธ DOM์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ตœ์ƒ์œ„ ํƒœ๊ทธ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
import React, { Fragment } from 'react'; export default function Test (){ return( <Fragment> <h1>Hello, world!</h1> <h2>React <h2> </Fragment> ) }
 
๋งŒ์•ฝ <div> ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
export default function Test (){ return( <div> <h1>Hello, world!</h1> <h2>React <h2> </div> ) }
notion image
์ด๋Ÿฐ ์‹์œผ๋กœ DOM์— divํƒœ๊ทธ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค. ์ด๋Ÿฐ ๊ฒƒ์„ Fragment๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
 

์ถ•์•ฝ ํ‘œํ˜„

import React from 'react'; export default function Test (){ return( <> <h1>Hello, world!</h1> <h2>React <h2> </> ) }
์ถ•์•ฝ ํ‘œํ˜„์œผ๋กœ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค! ํ•˜์ง€๋งŒ ์ถ•์•ฝ ํ‘œํ˜„์˜ key๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค.