Framework/React

[React] Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody> ์˜ค๋ฅ˜

yuri lee 2022. 10. 1. 00:55
๋ฐ˜์‘ํ˜•

Intro

์ „๋‹ฌ๋ฐ›์€ props๋ฅผ map ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ table๋ฅผ ๋งŒ๋“ค๋˜ ๋„์ค‘ ๋‹ค์Œ์˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>

 

<table>
    <thead>
        <tr>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
        </tr>
    </thead>
    <tbody>
        {props.map((item, index) => {
            return (
                <>
                    <tr key={index} onClick={}>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                    </tr>
                    <OtherComponent />
                </>
            );
        })}
    </tbody>
</table>

์ œ๊ฐ€ ๋ดค์„ ๋•Œ๋Š” ๋ถ„๋ช… ๊ฐ ์ž์‹์— index ๋ผ๋Š” key ๊ฐ’์„ ๋ถ€์—ฌํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ์š”..

 

Why?

ํ•˜์ง€๋งŒ..์•Œ๊ณ ๋ณด๋‹ˆ ์›์ธ์€ ๋ฐ”๋กœ <></> ์— ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.. key ๊ฐ’์€ root component์— ์ ์šฉ ๋˜๋Š” ๊ฒƒ์ด ์›์น™์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ํ•˜์ง€๋งŒ ์ €๋Š” OtherComponent ๋ฅผ ํ•จ๊ป˜ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ๊ธฐ์— <></> ํƒœ๊ทธ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. (JSX ๋ฌธ๋ฒ• ๐Ÿคง) 

 

 

How to solve the problem

<></> ์— key๋ฅผ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š”๊ฒŒ ๊ด€๊ฑด์ด์˜€์Šต๋‹ˆ๋‹ค. ๋ฆฌ์•กํŠธ ๊ณต์‹ ํŽ˜์ด์ง€์—์„œ key๊ฐ€ ์žˆ๋Š” Fragments ๋‚ด์šฉ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 

function Glossary(props) {
  return (
    <dl>
      {props.items.map(item => (
        // React๋Š” `key`๊ฐ€ ์—†์œผ๋ฉด key warning์„ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
        <React.Fragment key={item.id}>
          <dt>{item.term}</dt>
          <dd>{item.description}</dd>
        </React.Fragment>
      ))}
    </dl>
  );
}

 

๊ทธ๋Ÿฌ๋ฉด ๊ธฐ์กด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋˜ ์ฝ”๋“œ๋ฅผ ๊ณ ์ณ๋ด…์‹œ๋‹ค.

<table>
    <thead>
        <tr>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
            <th>test</th>
        </tr>
    </thead>
    <tbody>
        {props.map((item, index) => {
            return (
                <React.Fragment key={index}>
                    <tr key={index} onClick={}>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                        <td>{item}</td>
                    </tr>
                    <OtherComponent />
                </React.Fragment>
            );
        })}
    </tbody>
</table>

<></> ๋ฅผ Recat.Fragment๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  key ๊ฐ’์„ ๋„ฃ์–ด์คฌ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ์ ์šฉํ•˜๋‹ˆ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ์ •์ƒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. 

 


https://ko.reactjs.org/docs/fragments.html

https://kangdanne.tistory.com/147

๋ฐ˜์‘ํ˜•