let's get IT with DAVINA ๐ป
React๋ก TodoApp ๋ง๋ค๊ธฐ (2)Create ๊ธฐ๋ฅ ๊ฐ๋ฐ ๋ณธ๋ฌธ
DEV_IN/React
React๋ก TodoApp ๋ง๋ค๊ธฐ (2)Create ๊ธฐ๋ฅ ๊ฐ๋ฐ
๋ค๋น์น์ฝ๋๐ 2023. 3. 2. 22:58CSS ์์ ํ, CRUD์ ์ฒซ๋ฒ ์งธ ๊ธฐ๋ฅ์ธ Create ๊ธฐ๋ฅ ๊ตฌํ์ ๋ค์ด๊ฐ๋ค.
⊕ ๋ฒํผ์ ๋๋ฌ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ํญ๋ชฉ์ ์ถ๊ฐํ๋ ๊ธฐ๋ฅ!
- TodoInsert.js
- ๋ฐฐ๊ฒฝ์ ๋ด๋นํด์ฃผ๋ background div์
- form์ ํ๋ ๋ฃ์ด์ฃผ๊ธฐ (์ฌ์ฉ์๊ฐ ์ ๋ ฅํ๋ input + ์ ์ถํ๋ button ์์)
์ด๊ฑธ ์ฌ์ฉ์๊ฐ ⊕๋ฒํผ์ ๋๋ฅผ๋๋ง ๋ชจ๋ฌ์ ๋์ฐ๊ณ ์ถ์์์?
- App.js
import React, { useState } from "react";
import { MdAddCircle } from "react-icons/md";
import "./App.css";
import Template from "./components/Template";
import TodoInsert from "./components/TodoInsert";
import TodoList from "./components/TodoList";
//์ธ๋ถ์์ ์ ์ธํด์ฃผ๊ธฐ!
//๋ฐ์ ํจ์ ์์ ์์ผ๋ฉด ํจ์๊ฐ ๋ฆฌ๋๋๋ง ๋ ๋๋ง๋ค ๊ณ์ ์ฒ์๊ฐ์ผ๋ก ๋์๊ฐ๊ธฐ ๋๋ฌธ์..
let nextId = 4;
const App = () => {
const [insertToggle, setInsertToggle] = useState(false); //์ถ๊ฐ!
const [todos, setTodos] = useState([
{
id: 1,
text: "ํ ์ผ 1",
checked: true,
},
{
id: 2,
text: "ํ ์ผ 2",
checked: false,
},
{
id: 3,
text: "ํ ์ผ 3",
checked: true,
},
]);
//์ถ๊ฐ! toggle์ฐฝ ์ํ๋ฅผ ๋ฐ๊พธ๋ ํจ์!
const onInsertToggle = () => {
setInsertToggle((prev) => !prev);
};
//์ถ๊ฐ! ์
๋ ฅ๋ฐ์ ๊ฐ์ด ์์ผ๋ฉด alert, ์๋๋ฉด ์๋ก์ด ๋ฐฐ์ด return!
const onInsertTodo = (text) => {
if (text === "") {
return alert("ํ ์ผ์ ์
๋ ฅํด์ฃผ์ธ์.");
} else {
const todo = {
id: nextId,
text,
checked: false,
};
setTodos((todos) => todos.concat(todo)); //์ํ ๋ถ๋ณ์ฑ ์ ์ง push ๋์ concat
nextId++;
}
};
return (
<Template todoLength={todos.length}>
<TodoList
todos={todos}
onInsertToggle={onInsertToggle}
/>
<div className='add-todo-button' onClick={onInsertToggle}> //์ถ๊ฐ!
<MdAddCircle />
</div>
//insertToggle์ด true์ผ๋๋ง insert๋ชจ๋ฌ์ฐฝ ๋์์ฃผ๊ธฐ!
{insertToggle && (
<TodoInsert
onInsertToggle={onInsertToggle}
onInsertTodo={onInsertTodo}
/>
)}
</Template>
);
};
export default App;
- TodoInsert.js
import React, { useEffect, useState } from "react";
import { MdAddCircle } from "react-icons/md";
import { TiTrash, TiPencil } from "react-icons/ti";
import "./TodoInsert.css";
const TodoInsert = ({
onInsertToggle,
onInsertTodo
}) => {
const [value, setValue] = useState("");
//form์ ๊ฐ์ด ์ฌ์ฉ์๊ฐ ์
๋ ฅํ์ฌ ๋ฐ๋๋ฉด ํด๋น ๊ฐ์ผ๋ก value๊ฐ ๋ณ๊ฒฝํด์ฃผ๊ธฐ!
const onChange = (e) => {
setValue(e.target.value);
};
//form์ ๊ธฐ๋ณธ ์์ฑ, button type=submit์ผ๋ก ๋์ด์์ผ๋ ๋ฒํผ์ ์คํํ๋ฉด ์๋์ผ๋ก form์ ์๋ฒ์ ์ ์ถ
//์ด๋ ์๋ก๊ณ ์นจ์ด ์๋์ผ๋ก ๋์ด๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์, ์ด๋ฅผ ๋ง๋ e.preventDefault()
const onSubmit = (e) => {
e.preventDefault();
onInsertTodo(value);
setValue(""); //๋น๋ฌธ์์ด๋ก value๊ฐ ์ด๊ธฐํ
onInsertToggle(); //์ฐฝ ๋ค์ ๋ซ์์ฃผ๊ธฐ
};
return (
<div>
<div className='background' onClick={onInsertToggle}></div> //๋ฐฐ๊ฒฝ์ ํด๋ฆญํ๋ฉด ๋ชจ๋ฌ์ฐฝ ๋ซ๊ธฐ!
<form onSubmit={onSubmit}>
<input
placeholder='add your to-do'
value={value}
onChange={onChange}
></input>
<button type='submit'>
<MdAddCircle />
</button>
)}
</form>
</div>
);
};
export default TodoInsert;
โญ๏ธ ์ ์๋ก์ด ํญ๋ชฉ์ ์ถ๊ฐํ ๋, ๋ฐฐ์ด์ push๊ฐ ์๋ concat์ ์ผ์๊น?
setTodos((todos) => todos.concat(todo)); //์ํ ๋ถ๋ณ์ฑ ์ ์ง push ๋์ concat
push๋ฅผ ์ฐ๋ฉด ๊ธฐ์กด๋ฐฐ์ด ์์ฒด๊ฐ ๋ณ๊ฒฝ๋์ง๋ง,
concat์ ์ฐ๋ฉด ์๋ก์ด ๋ฐฐ์ด์ ๋ฆฌํดํ๋ ๊ธฐ์กด ๋ฐฐ์ด์ ๋ณ๊ฒฝ๋์ง ์์!
ex)
const arr1=[1,2,3,4] arr1.push(5) //5 console.log(arr1) //[1,2,3,4,5] const arr2=[1,2,3,4] arr2.concat(5) //[1,2,3,4,5] console.log(arr2) //[1,2,3,4]โ
ํด๋น ๊ธ์ ๋๋ฅด๋ฉด checked๋ ์ํ๊ฐ ๋ณ๊ฒฝํด์ฃผ๊ธฐ (์๋ฃ/๋ฏธ์๋ฃ ์ํ)
- App.js
//์ ํํ id์ todos.id์ ๊ฐ์ด ์ผ์นํ๋ฉด checked์ํ๋ง ๋ฐ๋๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ
const onCheckToggle = (id) => {
setTodos((todos) =>
todos.map((todo) =>
todo.id === id ? { ...todo, checked: !todo.checked } : todo
)
);
};
return (
<Template todoLength={todos.length}>
<TodoList
todos={todos}
onCheckToggle={onCheckToggle}
onInsertToggle={onInsertToggle}
onChangeSelectedTodo={onChangeSelectedTodo}
/>
...์๋ต
TodoList.js→TodoItem์ผ๋ก onCheckToggle์ props๋ก ๊ณ์ ์ ๋ฌํด์ค ๋ค,
import React from "react";
import { MdCheckBox, MdCheckBoxOutlineBlank } from "react-icons/md";
import "./TodoItem.css";
const TodoItem = ({
todo,
onCheckToggle,
onInsertToggle
}) => {
const { id, text, checked } = todo;
return (
<div className='TodoItem'>
<div className={`content ${checked ? "checked" : ""}`}>
{checked ? (
<MdCheckBox
onClick={() => {
onCheckToggle(id);
}}
/>
) : (
<MdCheckBoxOutlineBlank
onClick={() => {
onCheckToggle(id);
}}
/>
)}
<div
className='text'
onClick={() => {
onInsertToggle();
}}
>
{text}
</div>
</div>
</div>
);
};
export default TodoItem;
โญ๏ธ ์ด๋, onClick={onCheckToggle(id)}๋ก ํ๋ฉด ์๋๊ณ , onClick={()=>onCheckToggle(id)} ํํ๋ก ๋ณด๋ด์ค์ผ๋ผ!
'DEV_IN > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React Query (2) | 2023.03.24 |
---|---|
React๋ก TodoApp ๋ง๋ค๊ธฐ (3)Delete,Update ๊ธฐ๋ฅ ๊ฐ๋ฐ (2) | 2023.03.02 |
React๋ก TodoApp ๋ง๋ค๊ธฐ (1)Setup (4) | 2023.03.02 |
JSX (2) | 2023.02.21 |
What is React? (3) | 2023.02.21 |
Comments