๐ํ๊ฒฝ
Mac
VSCode
๐์ถ๊ฐํ๊ธฐ
`input`์ ํ ์ผ์ ์ ๋ ฅํ๊ณ ์ํฐ๋ฅผ ์ ๋ ฅ(๋๋ ์ถ๊ฐ ๋ฒํผ ํด๋ฆญ)ํ ์ผ ๋ชฉ๋ก์ด ์ถ๊ฐ๋์ด์ผ ํ๋ค.
function App() {
return (
<div>
<h1>ํ ์ผ ๋ชฉ๋ก</h1>
<form>
<input
type="text"
placeholder="ํ ์ผ"
/>
<button>์ถ๊ฐ</button>
</form>
</div>
);
}
export default App;
๊ธฐ๋ณธ ํ๊ทธ๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ , ๋ณ๊ฒฝ๋ ๊ฐ์ ์ถ์ ํ๊ธฐ ์ํ `useState`์ `onChange` ํจ์๋ฅผ ์ถ๊ฐํ๋ค.
const [toDo, setTodo] = useState("");
const [todoList, setTodoList] = useState([]);
function onChange(e) {
setTodo(e.target.value);
}
// ...
<form onSubmit={onSubmit}>
<input
onChange={onChange}
value={toDo}
type="text"
placeholder="ํ ์ผ"
/>
<button>์ถ๊ฐ</button>
</form>
์ด์ `input` ์ ๋ ฅ ๊ฐ์ ๋ณ๊ฒฝ์ด ๊ฐ์ง๋๋ฉด `todo`๊ฐ ๋ณ๊ฒฝ๋๋ค.
ํ ์ผ์ ์ ๋ ฅํ๋ฉด `input`์ด ๋ฆฌ์ ๋์ด์ผํ๊ณ , ์ ๋ ฅ๋ `todo`๋ฅผ ๋ชจ๋ ๊ฐ์ง๋ ๋ฆฌ์คํธ๊ฐ ์์ด์ผ ํ๋ค. `todoList`์ `onSubmit` ํจ์๋ฅผ ๋ง๋ค์ด์ค๋ค.
const [toDo, setTodo] = useState("");
const [todoList, setTodoList] = useState([]);
function onChange(e) {
setTodo(e.target.value);
}
function onSubmit(e) {
e.preventDefault();
if (toDo === "") {
return;
}
setTodoList((currentArray) => [toDo, ...currentArray]);
setTodo("");
}
useEffect(() => console.log(todoList), [todoList]);
// ...
<form onSubmit={onSubmit}>
<input
onChange={onChange}
value={toDo}
type="text"
placeholder="ํ ์ผ"
/>
<button>์ถ๊ฐ</button>
</form>
`setTodoList`์ ์ธ์๋ก ํจ์๊ฐ ์ ๋ฌ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค. `todo`๋ฅผ ๋ถ์ธ ์ ๋ฐฐ์ด๋ก `todoList`๋ฅผ ๋ณ๊ฒฝํด์ค์ผ ํ๊ธฐ ๋๋ฌธ์, ํ์ฌ์ `todoList`๋ฅผ ๋ถ๋ฌ์์(์์ ๋ณต์ฌ), `toDo`์ `todoList`๋ฅผ ๋ถ์ฌ์ค๋ค. ์ด๋ ๋ฐฐ์ด์ด `[toDo, currentArray]`๊ฐ ๋๋ฉด ๋ฐฐ์ด ์์ `currentArray` ๋ฐฐ์ด์ด ๋ค์ด๊ฐ ์ด์ํ ๋ชจ์(ํ ์ผ, [Array]) ์ด ๋๊ธฐ ๋๋ฌธ์ spread ์ฐ์ฐ์(...)๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์ด์ ์์๋ฅผ ๊บผ๋ด์์ผ ํ๋ค.
`todoList`๋ฅผ ์์ ๋ณต์ฌํ `currentArray`๊ฐ ํ ์ผ ๋ชฉ๋ก์ ๊ฐ์ง ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์, `...currentArray`๋ `[์ , ๋, ํ๋]` ์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
* ์ต์ ํ ์ผ์ด ํ๋จ์ ์ถ๊ฐ๋๋ ๊ฒ์ด ์ข๋ค๋ฉด `setTodoList((currentArray) => [toDo, ...currentArray]);`๋ก ์์ฑํ๋ฉด ๋๋ค.
๐ ์ถ๋ ฅํ๊ธฐ
ํ ์ผ์ ์ถ๊ฐํ์ผ๋ ๋ชฉ๋ก์ ๋ณด์ฌ์ค์ผํ๋ค. ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ return๋ฌธ ์์๋ ์ค๊ดํธ๋ฅผ ์ด์ด ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์๋ฐ์คํฌ๋ฆฝํธ์ `map()` ํจ์๋ฅผ ์ฌ์ฉํ์ฌ `todoList`์ ๋ชจ๋ ์์์ ๋ํด `<li>`ํ๊ทธ์ ๋ฃ์ด ์ถ๋ ฅํ ์ ์๋๋ก ์์ฑํ๋ค.
* ํ ์ผ ๋ชฉ๋ก์ด 0๊ฐ๋ผ๋ฉด `n๊ฐ`๊ฐ ์ถ๋ ฅ๋์ง ์๋๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
return (
<div>
<h3>
ํ ์ผ ๋ชฉ๋ก {todoList.length == 0 ? null : "| " + todoList.length + "๊ฐ"}
</h3>
<form onSubmit={onSubmit}>
<input
onChange={onChange}
value={toDo}
type="text"
placeholder="ํ ์ผ"
/>
<button>์ถ๊ฐ</button>
</form>
<hr />
{todoList.map((item, index) => (
<li key={index}>
{index + 1}. {item}
</li>
))}
</div>
);
๐ ์ญ์ ํ๊ธฐ
๋ชฉ๋ก ์ค ํ๋๋ฅผ ์ญ์ ํ๋ ค๋ฉด ์ถ๋ ฅ ์ ์ญ์ ๋ฒํผ์ ํจ๊ป ์ถ๋ ฅํด์ผํ๋ค. `map()` ํจ์ ์์ ์ญ์ ๋ฒํผ์ ์ถ๊ฐํด์ฃผ๊ณ , `onClick` ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํ๋ค. `todoList`๋ฅผ ๋ณ๊ฒฝํด์ผํ๋ฏ๋ก ๋ฐฐ์ด์ `filter()` ํจ์๋ฅผ ์ฌ์ฉํ๋ค.
function changeTodoList(e) {
console.log(e);
let item = e.target.value;
setTodoList((currentArray) =>
currentArray.filter((element) => element !== item)
);
}
// ...
return (
<div>
<h3>
ํ ์ผ ๋ชฉ๋ก {todoList.length == 0 ? null : "| " + todoList.length + "๊ฐ"}
</h3>
<form onSubmit={onSubmit}>
<input
onChange={onChange}
value={toDo}
type="text"
placeholder="ํ ์ผ"
/>
<button>์ถ๊ฐ</button>
</form>
<hr />
{todoList.map((item, index) => (
<li key={index}>
{index + 1}. {item}
<button value={item} onClick={changeTodoList}>
์ญ์
</button>
</li>
))}
</div>
);
๐์ฐธ๊ณ
ReactJS๋ก ์ํ ์น ์๋น์ค ๋ง๋ค๊ธฐ – ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders
React for Beginners
nomadcoders.co
์ ๊ฐ ๊ตฌ๋ฌธ - JavaScript | MDN
์ ๊ฐ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ด๋ ๋ฌธ์์ด๊ณผ ๊ฐ์ด ๋ฐ๋ณต ๊ฐ๋ฅํ ๋ฌธ์๋ฅผ 0๊ฐ ์ด์์ ์ธ์ (ํจ์๋ก ํธ์ถํ ๊ฒฝ์ฐ) ๋๋ ์์ (๋ฐฐ์ด ๋ฆฌํฐ๋ด์ ๊ฒฝ์ฐ)๋ก ํ์ฅํ์ฌ, 0๊ฐ ์ด์์ ํค-๊ฐ์ ์์ผ๋ก ๊ฐ์ฒด๋ก ํ์ฅ์
developer.mozilla.org
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] Can't resolve 'queryString' in ... (0) | 2025.03.13 |
---|---|
[React] ๋ฆฌ๋์ค(Redux) (0) | 2025.03.04 |
[React] ๊ธฐ์ด (0) | 2024.05.14 |