๋ฐ์ํ
๋ฆฌ์กํธ๋ฅผ ๊ฐ๋ฐํ๋ฉด์ ํ๋ฒ์ฏค์ ๋ณด์ จ์ ๊ฒฝ๊ณ ๋ฉ์ธ์ง... ๋ผ๊ณ ํ๋ค๐ฅ
map ํจ์์ ๋ํ ์ดํด๊ฐ ๋ถ์กฑํ์๋(์์งํ ์ง๊ธ๋ ๋ถ์กฑํจใ
ใ
) ๋ฐฐ์ด๊ฑธ ์ ์ฉํด๋ณด๋๋ผ ์ด์ฌํ ์ฝ์งํ๊ณ ์์์..
๐ช key ๊ฐ์ ์ฃผ์ง ์์ Comment.js
import React from 'react';
import './Comment.scss';
class Comment extends React.Component {
render() {
const { commentList } = this.props;
return (
<>
<ul className="comment_list">
{commentList.map(comment => {
return (
<li className="comment_box"> // โญ๏ธ key ๊ฐ์ด ์์!!
<span className="comment_id">{comment.userName}</span>
<span className="comment_content">{comment.content}</span>
</li>
);
})}
</ul>
</>
);
}
}
export default Comment;
๊ทธ๋ฐ๋ฐ.... npm start๋ฅผ ํ๋๋ ๊ฐ์๊ธฐ ์ด๋ฐ ์๋ฌ๊ฐ ๋ด๋ค. ๋๋ฅ!
ํ๊ตญ๋ง๋ก ๋ฒ์ญํ์๋ฉด..
โ๏ธ ๊ฒฝ๊ณ : ๋ชฉ๋ก์ ๊ฐ ํ์์๋ ๊ณ ์ ํ "ํค" ์ํ์ด ์์ด์ผ ํฉ๋๋ค.
์ด๋ด ๋ Javascript์์ ๋ฐฐ์ด ๊ฐ์ ํ๋์ฉ ๊บผ๋ด์ด return ํ ๋ ๋ฆฌ์กํธ๊ฐ ํญ๋ชฉ์ ์์ ์ ์ผ๋ก ์๋ณํ ์ ์๋๋ก ์์ฑ์ key ๊ฐ์ ์ถ๊ฐํ์ฌ return ํด ์ฃผ์ด์ผ ํ๋ค!!
๐ช key ๊ฐ์ ์ค ํ์ Comment.js
import React from 'react';
import './Comment.scss';
class Comment extends React.Component {
render() {
const { commentList } = this.props;
return (
<>
<ul className="comment_list">
{commentList.map(comment => {
return (
<li โญ๏ธkey={comment.id} className="comment_box">
<span className="comment_id">{comment.userName}</span>
<span className="comment_content">{comment.content}</span>
</li>
);
})}
</ul>
</>
);
}
}
export default Comment;
Error ๋ฉ์ธ์ง๊ฐ ์ฌ๋ผ์ก๋ค! ํธํธํธ
์ฐธ๊ณ ์ฌ์ดํธ ๐๐ป ๋ฆฌ์คํธ์ Key
๋ฐ์ํ
'๋ด๊ฐ ๋ง๋ ERROR' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Golang] go module error (0) | 2021.11.17 |
---|---|
[React] e.preventDefault(); (0) | 2021.11.16 |
[React] Invalid DOM property `class`. Did you mean `className`? (0) | 2021.11.16 |
[React] Too many re-renders. React limits the number of renders to prevent an infinite loop. (0) | 2021.11.16 |
[Git] error: src refspec main does not match any error: failed to push some refs to (github repo ์ฃผ์) (0) | 2021.11.16 |
๋๊ธ