
{
"id": 2,
"day": 1,
"eng": "apple",
"kor": "์ฌ๊ณผ",
"isDone": true
},
day, eng, kor๋ ์ ๋ ฅ๋ฐ๊ณ isDone์ false๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ์ ์ฅํ ์์
import React from "react";
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);
fetch(`http://localhost:3001/words/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
day: dayRef.current.value,
eng: engRef.current.value,
kor: korRef.current.value,
isDone: false,
}),
}).then((res) => {
if (res.ok) {
alert("์์ฑ์ด ์๋ฃ ๋์์ต๋๋ค");
}
});
}
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>
);
}
์ฌ๊ธฐ์,
function onSubmit(e) {
e.preventDefault();
console.log(engRef.current.value);
console.log(korRef.current.value);
console.log(dayRef.current.value);
fetch(`http://localhost:3001/words/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
day: dayRef.current.value,
eng: engRef.current.value,
kor: korRef.current.value,
isDone: false,
}),
}).then((res) => {
if (res.ok) {
alert("์์ฑ์ด ์๋ฃ ๋์์ต๋๋ค");
}
});
}
const engRef = useRef(null);
const korRef = useRef(null);
const dayRef = useRef(null);
์ ๋ฌํ
body์ ๊ฐ ์ ๋ฌ ๊ฐ์ ๋ฃ์ด์ค
useHistory()๋react-router์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ
const history= useHistory();
history.push(์ํ๋ ์ด๋ url);
useHistory()๋ก ์ค์ ํhistory์ ์ฃผ์๋ฅผpushํ๋ฉด ํด๋น ํ์ด์ง๋ก ์ด๋
import React from "react";
import { useRef } from "react";
import { useHistory } from "react-router-dom";
import useFetch from "../hooks/useFetch";
export default function CreateWord() {
const days = useFetch("http://localhost:3001/days");
const history = useHistory();
function onSubmit(e) {
e.preventDefault();
fetch(`http://localhost:3001/words/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
day: dayRef.current.value,
eng: engRef.current.value,
kor: korRef.current.value,
isDone: false,
}),
}).then((res) => {
if (res.ok) {
alert("์์ฑ์ด ์๋ฃ ๋์์ต๋๋ค");
history.push(`/day/${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>
);
}
import { useHistory } from "react-router-dom";
import useFetch from "../hooks/useFetch";
export default function CreateDay() {
const days = useFetch("http://localhost:3001/days");
const history = useHistory();
function addDay(e) {
e.preventDefault();
fetch(`http://localhost:3001/days/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
day: days.length + 1,
}),
}).then((res) => {
if (res.ok) {
alert("์์ฑ์ด ์๋ฃ ๋์์ต๋๋ค");
history.push(`/`);
}
});
}
return (
<div>
<h3> ํ์ฌ ์ผ์ : {days.length}์ผ</h3>
<button onClick={addDay}> Day ์ถ๊ฐ </button>
</div>
);
}