let's get IT with DAVINA ๐Ÿ’ป

React๋กœ TodoApp ๋งŒ๋“ค๊ธฐ (1)Setup ๋ณธ๋ฌธ

DEV_IN/React

React๋กœ TodoApp ๋งŒ๋“ค๊ธฐ (1)Setup

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 3. 2. 21:14
๋ถ€ํŠธ์บ ํ”„๊ฐ€ ๋๋‚˜๊ณ  ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋” ์‹ฌํ™”๋˜๊ณ , ์ƒˆ๋กœ์šด ์–ธ์–ด ๊ธฐ๋ฐ˜์œผ๋กœ, ๋” ์–ด๋ ค์šด ์ˆ˜์ค€์˜ ์ถ”๊ฐ€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๋“ฏ ํ•˜์—ฌ ๋Œ€๋‹จํ•ด๋ณด์ธ๋‹ค.. ๋ฌผ๋ก  ์ฝ”๋”ฉ์ด๋ž€๊ฑธ ์ฒ˜์Œ ์‹œ์ž‘ํ–ˆ์„ ๋•Œ "๋‚˜"๋ณด๋‹ค๋Š” ํ˜„์žฌ์˜ "๋‚ด"๊ฐ€ ๋” ๋ฐœ์ „ํ•ด์žˆ๋Š” ๋ชจ์Šต์ด๊ณ , ๋ถˆ์•ˆํ•ดํ•˜์ง€ ๋ง์ž๊ณ  ๋งค์ผ ๋‹ค์งํ•˜์ง€๋งŒ ๋‚œ ๋‚ด๊ฐ€ ๊ธฐ๋ณธ๊ธฐ๊ฐ€ ์—ฌ์ „ํžˆ ๋งŽ์ด ๋ถ€์กฑํ•˜๊ตฌ๋‚˜๋ฅผ ๊ณ„์† ๊นจ๋‹ฌ์œผ๋ฉฐ ๋งŽ์ด ์ขŒ์ ˆํ•˜๋Š” ์š”์ฆ˜์ธ๊ฒƒ๊ฐ™๋‹ค.. ๊ทธ ์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์“ฐ๊ฑด ํ•˜๋‚˜์˜ ํ™ˆํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ๋•Œ, CRUD๋ฅผ ์ •๋ง ์™„๋ฒฝํ•˜๊ฒŒ ์†์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€๋ฅผ ์Šค์Šค๋กœ์—๊ฒŒ ๋ฌผ์–ด๋ณธ๋‹ค๋ฉด ๋„ˆ๋ฌด๋‚˜๋„ ๋‹น์—ฐํžˆ NO์˜€๋‹ค.. ๊ทธ๋ž˜์„œ ๋‚œ ๋” ์–ด๋ ต๊ธฐ๋ณด๋‹จ ๊ธฐ๋ณธ๊ธฐ๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋‹ฆ๊ณ ์ž ํ”„๋กœ์ ํŠธ ์ „ ๋ถ€ํŠธ์บ ํ”„์—์„œ ๊ณผ์ œ๋กœ ์คฌ๋˜ (๋‚œ ์‹คํŒจํ–ˆ๋˜) Todo App์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด๋ณด๊ณ ์ž ์‹œ์ž‘ํ–ˆ๋‹ค.

1. React Component ๋งŒ๋“ค๊ธฐ

React๋กœ ๊ฐœ๋ฐœ ์‹œ์ž‘ํ•˜๊ธฐ

npx create-react-app todo-bin

  • Template.js & Template.css
    • ์ „์ฒด ํ…œํ”Œ๋ฆฟ, ๋ผˆ๋Œ€, ๊ตฌ์กฐ๋ฅผ ๋‹ด๋‹น 
  • TodoInsert.js & TodoInsert.css
    • ๊ธ€ ์ถ”๊ฐ€ (CREATE) ๊ธฐ๋Šฅ์šฉ ์ปดํฌ๋„ŒํŠธ
    • pop-up(Modal)์ฐฝ์ฒ˜๋Ÿผ ๋„์šธ๊ผฌ์•ผ
  • TodoItem.js & TodoItem.css
    • ํ•˜๋‚˜ํ•˜๋‚˜์˜ ๊ธ€ ๊ด€๋ฆฌ ์ปดํฌ๋„ŒํŠธ
  • TodoList.js & TodoList.css
    • ์ „์ฒด ๊ธ€ ๊ด€๋ฆฌ ์ปดํฌ๋„ŒํŠธ
  • App.js & App.css
    • ์ „์ฒด ํ™”๋ฉด ๋ Œ๋”๋ง ์ปดํฌ๋„ŒํŠธ

2. ๊ธฐ๋ณธ ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

  • App.js
    • ์ผ๋‹จ App.js๋ฅผ ๋ฐ‘์—์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด ๋นˆ ํ™”๋ฉด์— "์•ˆ๋…•ํ•˜์„ธ์š”"๋งŒ ๋œจ๊ณ  ์žˆ์–ด์š”

  • Template.js
    • ๋ผˆ๋Œ€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฑธ ๋งŒ๋“ค์–ด์ค˜์š”. ์œ„์— App.js <div>์— ๋„ฃ์–ด์ค„๊บผ์˜ˆ์š”.

import React from "react";
import "./Template.css";

const Template = ({ children}) => {
    return (
        <div className='Template'>
            <div className='title'>์˜ค๋Š˜์˜ ํ•  ์ผ (0)</div>
            <div>{children}</div>
        </div>
    );
};

export default Template;

์ด ๋•Œ, Template์— children์„ ์ „๋‹ฌ์ธ์ž๋กœ ์ „ํ•ด์ค˜์„œ ์˜ค๋Š˜์˜ ํ•  ์ผ ๋ฐ‘์— ์ปดํฌ๋„ŒํŠธ๋กœ ๋„์›Œ์ฃผ๋ฉด

App.js์—์„œ ์ ์šฉํ•  ๋•Œ  <Template>์•ˆ๋…•ํ•˜์„ธ์š”</Template> OR <Template><TodoList/></Template> ์ฒ˜๋Ÿผ Template์‚ฌ์ด์— ๋“ค์–ด๊ฐ€๋Š” ๋ฌธ์ž์—ด์ด๋‚˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ ๋“ฑ์„ ํ™”๋ฉด์— ๊ทธ๋Œ€๋กœ ๋„์›Œ์ค๋‹ˆ๋‹ค. 


  • App.js
import React, { useState } from "react";
import "./App.css";
import Template from "./components/Template";
import TodoInsert from "./components/TodoInsert";
import TodoList from "./components/TodoList";

const App = () => {

	//React Hook์„ ์ด์šฉํ•ด ๊ธฐ๋ณธ ํ• ์ผ ๋ชฉ๋ก ์ •์˜
    const [todos, setTodos] = useState([
        {
            id: 1,
            text: "ํ• ์ผ 1",
            checked: true,
        },
        {
            id: 2,
            text: "ํ• ์ผ 2",
            checked: false,
        },
        {
            id: 3,
            text: "ํ• ์ผ 3",
            checked: true,
        },
    ]);

    return (
        <Template todoLength={todos.length}>
            <TodoList
                todos={todos} //todos๋ฆฌ์ŠคํŠธ ๋ณด๋‚ด์ฃผ๊ธฐ
            />
        </Template>
    );
};

export default App;
  • Template.js
import React from "react";
import "./Template.css";

const Template = ({ children, todoLength }) => {
    return (
        <div className='Template'>
            <div className='title'>์˜ค๋Š˜์˜ ํ•  ์ผ ({todoLength})</div>
            <div>{children}</div>
        </div>
    );
};

export default Template;
  • TodoList.js
    • ์ด ๋•Œ, map์„ ์ด์šฉํ•˜๋ฉด ๊ณ ์œ ํ•œ id๊ฐ’์„ ๊ผญ key์— ๋„ฃ์–ด์ค˜์•ผ warning์ด ์•ˆ๋œฌ๋‹ค!
import React from "react";
import TodoItem from "./TodoItem";
import "./TodoList.css";

const TodoList = ({todos}) => {
    return (
        <div className='TodoList'>
            {todos.map((todo) => ( //map์„ ํ†ตํ•ด ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฟŒ๋ ค์ฃผ๊ธฐ
                <TodoItem
                    key={todo.id}
                    todo={todo}
                />
            ))}
        </div>
    );
};

export default TodoList;
  • TodoItem.js
    • ๊ฐ์ฒด ๋ถ„ํ•ด ํ• ๋‹น์„ ์ด์šฉํ•ด์„œ text๋ถ€๋ถ„ "ํ• ์ผ1","ํ• ์ผ2","ํ• ์ผ3"์„ ํ™”๋ฉด์— ๋„์›Œ์ฃผ๊ธฐ

๊ทธ๋Ÿผ ๋”ฐ๋‹จํ•˜๊ณ  ์™„์„ฑ


โ“โ“โ“ React Hook?

ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ state๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ!!

Comments