๐Ÿ’ป[React Native] Expo๋กœ TodoList ๋งŒ๋“ค๊ธฐ2

ํ˜ฑยท2023๋…„ 1์›” 15์ผ

๐Ÿ“Œ Expo checkbox

React์˜ switch์™€ ์œ ์‚ฌ

๐Ÿ“Œ Row Component

style์„ ๋ฏธ๋ฆฌ component ์ž์ฒด๋กœ ๋นผ์„œ ์ ์šฉ
์ด๋•Œ, ์›ํ•˜๋Š” ์›๋ž˜์˜ style+์ž์ฒด๋กœ ๋”ฐ๋กœ ์ž…๋ ฅ๋ฐ›๊ณ  ์‹ถ์€ style๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ ์šฉ

๐Ÿ”— Row.js

function Row({ children, style }) {
  return <View style={[ styles.row, style ]}> //style ๋”ฐ๋กœ ์ž…๋ ฅ ๋ฐ›๊ธฐ
    {children}
    </View>;
}
const styles = StyleSheet.create({ //default style
  row: {
    flexDirection: "row",
  }
});
export default Row;

๐Ÿ”— TodoItem.js

export default function TodoItem() {
  return (
    <Row style={{ alignItems: "center" }}>
      <Checkbox />
      <Text>ํ•  ์ผ ๋ชฉ๋ก</Text>
    </Row>
  );
}

๐Ÿ“Œ checkbox

falsyํ•œ ๋ถ€๋ถ„์œผ๋กœ, ํด๋ฆญ์„ ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ”๋กœ UI๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š์Œ
value๊ฐ’ ์ž์ฒด๊ฐ€ true๋กœ ๋ฐ”๋€Œ์–ด์•ผ๋งŒ UI ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚จ
๋”ฐ๋ผ์„œ useState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, Event๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์ด ๋ณ€๊ฒฝ์ด ๋˜๊ณ , ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด state์— ๋ฐ˜์˜์„ ํ•˜๊ณ  state๊ฐ’์ด ๋‹ค์‹œ checkbox์— ๋ฐ˜์˜ํ•˜์—ฌ UI ๋ณ€ํ™”๊ฐ€ ๋‚˜ํƒ€๋‚จ

๐Ÿ”— TodoItem.js

export default function TodoItem() {
  const [isDone, setDone] = useState(false);
  return (
    <Row style={{ alignItems: "center", marginBottom: 12 }}>
      <Checkbox
        value={isDone}
        onValueChange={(value) => setDone(value)}
        valuestyle={{ marginRight: 8 }}
      />
      <Text>ํ•  ์ผ ๋ชฉ๋ก</Text>
    </Row>
  );
}

๐Ÿ“Œ ๊ธ€์ž ์ž…๋ ฅ๋ฐ›๊ธฐ - {label}

๐Ÿ”— TodoItem.js

export default function TodoItem({ label }) {
  const [isDone, setDone] = useState(false);
  return (
    <Row style={{ alignItems: "center", marginBottom: 12 }}>
      <Checkbox
        value={isDone}
        onValueChange={(value) => setDone(value)}
        valuestyle={{ marginRight: 8 }}
      />
      <Text style={{ color: isDone ? "#eee" : "#000" }}>
        {label ?? "๋‚ด์šฉ ์—†์Œ"}
      </Text>
    </Row>
  );
}

๐Ÿ“Œ map() -> ๋ฆฌ์ŠคํŠธํ˜• ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ

๐Ÿ”— App.js

const [list, setList] = useState(["ํ• ์ผ 1", "ํ• ์ผ 2"]);
return (
  <SafeAreaView style={styles.container}>
    <Padding padding={12}>
      {list.map((item) => (
        <TodoItem key={item} label={item} />
      ))}
  • key: ๊ฐ ์š”์†Œ๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ, ์ •ํ™•ํ•˜๊ฒŒ ๋˜‘๊ฐ™์€ component๋‚˜์—ด์ผ ๋•Œ ์ค‘๊ฐ„์— ์–ด๋–ค ๊ฒƒ์ด ๋น ์ ธ๋„ ๋ฐ”๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Œ, ๊ตฌ๋ถ„์„ ํ•  ์ˆ˜ ์žˆ์–ด์„œ ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๋Š” ์„ฑ๋Šฅ์„ ๋†’์—ฌ์คŒ

    ๋ช…ํ™•ํ•˜๊ฒŒ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๋Š” KEY๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด ๋” ํ˜„๋ช…ํ•œ ๋ฐฉ๋ฒ•
    INDEX๋Š” ์š”์†Œ ํ•˜๋‚˜๊ฐ€ ์ œ๊ฑฐ๊ฐ€ ๋˜๋ฉด, ๋‹ค์‹œ ๋ถ€์—ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์Œ

profile
new blog: https://hae0-02ni.tistory.com/

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