React/Velopert's React

[React] Study #10 | ๋ฐฐ์—ด ๋ Œ๋”๋ง, key์˜ ์ค‘์š”์„ฑ

yevdev 2021. 10. 2. 16:41

๐Ÿ’กIDEA

  1. ๊ทธ๋ƒฅ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ (๋”ฐ๋กœ ์ •๋ฆฌํ•˜์ง€ ์•Š๊ฒ ์Œ, ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์„œ ใ…œ )
  2. ํ•œํŒŒ์ผ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ ์„ ์–ธ

 

1๏ธโƒฃ ๋ฐฐ์—ด์ด ๊ณ ์ •์ ์ผ ๊ฒฝ์šฐ, ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์กฐํšŒํ•ด๊ฐ€๋ฉด์„œ ๋ Œ๋”๋งํ•˜๋Š” ๋ฐฉ๋ฒ• (์ •์ ์ธ ๋ฐฐ์—ด)

import React from 'react'

function User({user}) {
    return(
        <div>
            <span>{user.name}</span>
            <span>{user.age}</span>
        </div>
    )
}

function UserList() {

    // ๋ Œ๋”๋ง์„ ์œ„ํ•œ ๋ฐฐ์—ด
    const users = [
        {id: 1, name: 'John', age:19},
        {id: 2, name: 'Car', age:20},
        {id: 3, name: 'lee', age:14},
    ];

    return (
        <div>
            <User user={users[0]} />
            <User user={users[1]} />
            <User user={users[2]} />
        </div>
    )
}

export default UserList

 

2๏ธโƒฃ ๋™์ ์ธ ๋ฐฐ์—ด์„ ๋ Œ๋”๋ง

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด์˜ ๋‚ด์žฅํ•จ์ˆ˜ map() ์„ ์‚ฌ์šฉ

๐Ÿ’ก map() ํ•จ์ˆ˜?

  • ๋ฐฐ์—ด์•ˆ์— ์žˆ๋Š” ๊ฐ ์›์†Œ๋ฅผ ๋ณ€ํ™˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
  • ๋ฆฌ์•กํŠธ์—์„œ ๋™์ ์ธ ๋ฐฐ์—ด์„ ๋ Œ๋”๋งํ•ด์•ผ ํ•  ๋•Œ๋Š” ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์„ ๋ฆฌ์•กํŠธ ์•จ๋ฆฌ๋จผํŠธ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค!
import React from 'react'

function User({user}) {
    return(
        <div>
            <span>{user.name}</span>
            <span>{user.age}</span>
        </div>
    )
}

function UserList() {

    const users = [
        {id: 1, name: 'John', age:19},
        {id: 2, name: 'Car', age:20},
        {id: 3, name: 'lee', age:14},
    ];

    return (
        <div>
            {/* map() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ */}
            {users.map(user => (
                <User user={user} />
            ))}
        </div>
    )
}

export default UserList

 

 

 

๐Ÿšซ ์ด๋•Œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์ด์žˆ๋‹ค, ๋ฆฌ์•กํŠธ์—์„œ ๋ฐฐ์—ด์„ ๋ Œ๋”๋งํ•  ๋•Œ๋Š” key๋ผ๋Š” props๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ!

  • key ๊ฐ’์€ ์›์†Œ๋“ค๋งˆ๋‹ค ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณ ์œ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์•ผํ•œ๋‹ค.
  • ๋งŒ์•ฝ ๋ฐฐ์—ด์„ ๋ Œ๋”๋ง ํ• ๋•Œ, key ์„ค์ •์„ ํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค๋ฉด, ๊ธฐ๋ณธ์ ์œผ๋กœ index ๊ฐ’์„ key๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๊ณ , ๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๊ฒŒ ๋œ๋‹ค.
  • ๊ฐ ๊ณ ์œ  ์›์†Œ์— key๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ๋ฐฐ์—ด์ด ์—…๋ฐ์ดํŠธ ๋ ๋•Œ ํšจ์œจ์ ์œผ๋กœ ๋ Œ๋”๋ง์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ์ง€๊ธˆ์˜ ๊ฒฝ์šฐ์—๋Š” id๊ฐ€ ๊ณ ์œ  ๊ฐ’์ž„!
import React from 'react'

function User({user}) {
    return(
        <div>
            <span>{user.name}</span>
            <span>{user.age}</span>
        </div>
    )
}

function UserList() {

    const users = [
        {id: 1, name: 'John', age:19},
        {id: 2, name: 'Car', age:20},
        {id: 3, name: 'lee', age:14},
    ];

    return (
        <div>
            {/* ๋ฐฐ์—ด์„ ๋ Œ๋”๋งํ• ๋•Œ, key๋ผ๋Š” props๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ธฐ!! */}
            {users.map(user => (
                <User user={user} key={user.id}/>
            ))}
        </div>
    )
}

export default UserList

 

  • ๋งŒ์•ฝ, ๋ฐฐ์—ด์•ˆ์˜ ์›์†Œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณ ์œ ๊ฐ’์ด ์„ค์ •์ด ์•ˆ๋˜์–ด์žˆ๋‹ค๋ฉด map() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์„ค์ •ํ•˜๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ index๋ฅผ key๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
<div>
  {users.map((user, index) => (
    <User user={user} key={index} />
  ))}
</div>

 

 

โžฐ Key์˜ ์กด์žฌ์œ ๋ฌด์— ๋”ฐ๋ฅธ ์—…๋ฐ์ดํŠธ ๋ฐฉ์‹

1๏ธโƒฃ key๊ฐ€ ์—†์„ ๊ฒฝ์šฐ

  • ๋น„ํšจ์œจ์ ์ธ ์—…๋ฐ์ดํŠธ
  • ๋ฐฐ์—ด๋“ค ์‚ฌ์ด์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์‚ฝ์ž… ํ˜น์€ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์ธ๋ฑ์Šค์˜ ๊ฐ’๋“ค์— ๋ณ€๋™์ด ์ผ์–ด๋‚˜๊ฒŒ ๋œ๋‹ค.

2๏ธโƒฃ key๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ

  • ํšจ์œจ์ ์ธ ์—…๋ฐ์ดํŠธ
  • ๋ฐฐ์—ด๋“ค ์‚ฌ์ด์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์‚ฝ์ž… ํ˜น์€ ์‚ญ์ œํ• ๋•Œ, ์ˆ˜์ •๋˜์ง€ ใ…‡๋‚ณ๋Š” ๊ธฐ์กด์˜ ๊ฐ’์€ ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ์›ํ•˜๋Š” ๊ณณ์— ๋‚ด์šฉ์„ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋•Œ๋ฌธ์—, ๋ฐฐ์—ด์„ ๋ Œ๋”๋ง ํ•  ๋–„์—๋Š” ๊ณ ์œ ํ•œ key ๊ฐ’์ด ์žˆ๋Š” ๊ฒƒ์ด ์ค‘์š”
  • ๋งŒ์•ฝ ๋ฐฐ์—ด์•ˆ์— ์ค‘๋ณต๋˜๋Š” key๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ๋ Œ๋”๋ง ์‹œ์— ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๊ฐ€ ์ฝ˜์†”์— ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋˜๋ฉฐ, ์—…๋ฐ์ดํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ์ด๋ฃจ์–ด ์ง€์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

 


 

Reference

https://react.vlpt.us/basic/11-render-array.html