
export default function CreateWord() {
return (
<form>
<div className="input_area">
<label>Eng</label>
<input type="text" placeholder="computer" />
</div>
<div className="input_area">
<label>Kor</label>
<input type="text" placeholder="์ปดํจํฐ" />
</div>
<div className="input_area">
<label>Day</label>
<select>
<option>1</option>
<option>2</option>
</select>
</div>
<button>์ ์ฅ</button>
</form>
);
}
์ ๋ ฅ๋ฐ๋
<form>ํ๊ทธ ์ด์ฉ!
import useFetch from "../hooks/useFetch";
export default function CreateWord() {
const days = useFetch("http://localhost:3001/days");
return (
<form>
<div className="input_area">
<label>Eng</label>
<input type="text" placeholder="computer" />
</div>
<div className="input_area">
<label>Kor</label>
<input type="text" placeholder="์ปดํจํฐ" />
</div>
<div className="input_area">
<label>Day</label>
<select>
{days.map((day) => (
<option key={day.id} value={day.day}>
{day.day}
</option>
))}
</select>
</div>
<button>์ ์ฅ</button>
</form>
);
}
์์ ๋ง๋ค์ด ๋์๋
useFetch()๋ฅผ ํ์ฉํ์ฌ, day์ data๋ฅผ ๋ถ๋ฌ์ด
๊ฐ day๋ค์ ์ ํํ๋ ํํ๋ก ๋ค์ ๋์ดํด์ผ ํ๋ฏ๋ก, ์ฆ ํํ๋ง ๋ณํ! => mapํจ์ ์ฌ์ฉ
import useFetch from "../hooks/useFetch";
export default function CreateWord() {
const days = useFetch("http://localhost:3001/days");
function onSubmit(e) {
e.preventDefault();
}
return (
<form onSubmit={onSubmit}>
<div className="input_area">
<label>Eng</label>
<input type="text" placeholder="computer" />
</div>
<div className="input_area">
<label>Kor</label>
<input type="text" placeholder="์ปดํจํฐ" />
</div>
<div className="input_area">
<label>Day</label>
<select>
{days.map((day) => (
<option key={day.id} value={day.day}>
{day.day}
</option>
))}
</select>
</div>
<button>์ ์ฅ</button>
</form>
);
}
event๊ฐ ์๊ฒจ๋ ์๋ก ๊ณ ์นจ์ ํ์ง ์๋๋ก,
preventDefault()๋ฅผ ์ฌ์ฉ
formํ๊ทธ๋ก ๊ฐ์ธ์ ธ ์์ด, ์๋ก๊ณ ์นจ์ด ๋๋ ๊ฒ์ด๋ฏ๋กform์onSubmitํจ์ ์ถ๊ฐ
=> ๊ธฐ๋ณธ ๊ธฐ๋ฅ์ ๋ง์ ์ฃผ๋ ๊ฒ
input์ฐฝ์ ์ ํ ๊ฐ๋ค์ ์ป๊ธฐ ์ํด์
useRef()์ฌ์ฉ
useRef()๋ DOM์ ์ ๊ทผํ ์ ์๋๋ก ํจ (์คํฌ๋กค ์์น ํ์ธ, ํฌ์ปค์ค ์ค ๋ ๋ฑ ์ฌ์ฉ), DOM ์์๊ฐ ์์ฑ๋ ํ ์ ๊ทผํ ์ ์์
import { useRef } from "react";
import useFetch from "../hooks/useFetch";
export default function CreateWord() {
const days = useFetch("http://localhost:3001/days");
function onSubmit(e) {
e.preventDefault();
console.log(engRef.current.value);
console.log(korRef.current.value);
console.log(dayRef.current.value);
}
const engRef = useRef(null);
const korRef = useRef(null);
const dayRef = useRef(null);
return (
<form onSubmit={onSubmit}>
<div className="input_area">
<label>Eng</label>
<input type="text" placeholder="computer" ref={engRef} />
</div>
<div className="input_area">
<label>Kor</label>
<input type="text" placeholder="์ปดํจํฐ" ref={korRef} />
</div>
<div className="input_area">
<label>Day</label>
<select ref={dayRef}>
{days.map((day) => (
<option key={day.id} value={day.day}>
{day.day}
</option>
))}
</select>
</div>
<button>์ ์ฅ</button>
</form>
);
}
๊ฐ ์ ๋ ฅ ๋ถ๋ถ์ ๋ํ
Ref()์์ฑ ํ ์ฐ๊ฒฐ
DOM ์์ ์์ฑ ์ดํ ์ ๊ทผ ๊ฐ๋ฅ
์ ์ฅ ๋ฒํผ -> ๋ ๋๋ง ๊ฒฐ๊ณผ๊ฐ DOM์ ๋ฐ์๋ ์ดํ๋ฅผ ๋ณด์ฌ์ค
const engRef = useRef(null);์ ๋ ฅํ๊ณ , ์ ์ฅ๋ฒํผ์ ๋๋ ์ ๋ ๊ทธ์ ๋ํ ์ ๋ ฅ ๊ฒฐ๊ณผ์ ์ ๊ทผํ ์ ์์
current์์ฑ = ํด๋น ์์์ ์ ๊ทผ
value= input์ ์ ๋ ฅ๋ ๊ฐ