React-Native Tutorial To do list [ #13 ]

์กฐํŒ”๋กœยท2020๋…„ 9์›” 22์ผ
0

React-Native-Todolist

๋ชฉ๋ก ๋ณด๊ธฐ
13/15
post-thumbnail

React Native Tutorial ๋”ฐ๋ผํ•˜๊ธฐ ๐Ÿ“ฑ

๐Ÿ“Œ ํŠœํ† ๋ฆฌ์–ผ์„ ๋”ฐ๋ผํ•œ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค!

ํ•  ์ผ ๋ชฉ๋ก ์ถ”๊ฐ€ํ•˜๊ธฐ - TodoList

app.js ํŒŒ์ผ์—์„œ todos๋ฅผ TodoList ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌํ•˜๊ธฐ

<TodoList todos={todos} />

TodoList๋กœ todos ๋ฐ›๊ธฐ

์ฃผ์˜์‚ฌํ•ญ

todos๋Š” ํ•  ์ผ ๋ชฉ๋ก์˜ ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ TodoList์ปดํฌ๋„ŒํŠธ์—์„œ TodoListItem ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌ ํ•  ๋•Œ์—๋Š” ๋ฐฐ์—ด์— ๋‹ด๊ธด ๊ฐ์ฒด ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋„˜๊ฒจ์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

const TodoList = ({todos}) => {
  return (
    <ScrollView contentContainerStyle={styles.listContainer}>
      {todos.map(todo => (
        <TodoListItem {...todo} />
      ))}
    </ScrollView>
  );
};

์„ค๋ช…

map() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ todos์— ๋‹ด๊ธด ์•„์ดํ…œ์„ ํ•˜๋‚˜์”ฉ TodoListItem ์ปดํฌ๋„ŒํŠธ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง(destructuring) ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜๋ฉด TodoListItem ์ปดํฌ๋„ŒํŠธ์—์„œ ์•„์ดํ…œ ๊ฐ์ฒด์— ๋‹ด๊ธด ๊ฐ’๋“ค์„ ๋ฐ”๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ์— textValue ๊ฐ’ ๋„ฃ๊ธฐ

const TodoListItem = ({textValue, id, checked}) => {
    return (
        <View style={styles.container}>
            <TouchableOpacity>
                <View style={styles.completeCircle}>
                <View style={styles.completeCircle}>
                    <Icon name="circledowno" size={30} color="#3143e8" />
                </View>
                </View>
            </TouchableOpacity>
            <Text style={[styles.text, styles.strikeText]}>{textValue}</Text>
            <TouchableOpacity style={styles.buttonContainer}>
                <Text style={styles.buttonText}>
                    <Icon name="delete" size={30} color="#e33057" />
                </Text>
            </TouchableOpacity>
        </View>
    );
};

๊ฒฐ๊ณผ๋ณด๊ธฐ

๊ฒฝ๊ณ ์ฐฝ

์„ค๋ช…

๊ทธ๋Ÿฐ๋ฐ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ ํ•˜๋‹จ์— ๊ฒฝ๊ณ ์ฐฝ์ด ๋œจ๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฝ๊ณ  ๋ฌธ๊ตฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Each child in a list should have a unique 'key' prop.

์ฆ‰, ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ์•„์ดํ…œ ๊ฐ์ฒด๋งˆ๋‹ค ๊ณ ์œ ํ•œ key๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๋ง ์ž…๋‹ˆ๋‹ค.

์•ž์„œ ์ƒˆ๋กœ์šด ์•„์ดํ…œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋žœ๋ค์œผ๋กœ id ๊ฐ’์„ ๋งŒ๋“ค์–ด์ค€ ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์‹œ๋‚˜์š”? id ๊ฐ’์„ ๋งŒ๋“ค์–ด ์ค€ ์ด์œ ๊ฐ€ ๋ฐ”๋กœ ์—ฌ๊ธฐ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ์•„์ดํ…œ์„ ์ถœ๋ ฅํ•  ๋•Œ์—๋Š” ๊ฐ ์•„์ดํ…œ์˜ ๊ณ ์œ  ๋ฒˆํ˜ธ๋ฅผ key ๊ฐ’์œผ๋กœ ์ฃผ์–ด์•ผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (์ €ํฌ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ Math.random()์œผ๋กœ id๋ฅผ ๋งŒ๋“ค์—ˆ์ง€๋งŒ, ์ด๋Š” ์ค‘๋ณต๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹ค์ œ ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” uuid์™€ ๊ฐ™์€ ํŒจํ‚ค์ง€ ๋“ฑ์„ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” id๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.)

๊ฒฝ๊ณ  ํ•ด๊ฒฐํ•˜๊ธฐ

TodoList ์ปดํฌ๋„ŒํŠธ๋กœ ๋Œ์•„๊ฐ€์„œ TodoListItem์— id ๊ฐ’๋„ ๊ฐ™์ด ๋„˜๊ฒจ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

<ScrollView contentContainerStyle={styles.listContainer}>
      {todos.map(todo => (
        <TodoListItem key={todo.id} {...todo} />
      ))}
</ScrollView>  

๊ฒฐ๊ณผ๋ณด๊ธฐ

์ฃผ์˜์‚ฌํ•ญ

Error: Unable to resolve module `./debugger-ui/debuggerWorker.cff11639.js` from ``:
์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด
npm install @react-native-community/cli-debugger-ui --save
์ด ๋ช…๋ น์–ด

profile
ํ˜„์‹ค์— ์•ˆ์ฃผํ•˜์ง€ ์•Š๊ณ  - ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€