
React์ switch์ ์ ์ฌ
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> ); }
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> ); }
๐ 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> ); }
๐ 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๋ ์์ ํ๋๊ฐ ์ ๊ฑฐ๊ฐ ๋๋ฉด, ๋ค์ ๋ถ์ฌ๋๊ธฐ ๋๋ฌธ์ ํท๊ฐ๋ฆด ์ ์์