
๋๋ฆฐ ์ธํฐ๋ท ํ๊ฒฝ TEST ๊ฐ๋ฅ
๋๋ฆฐ ์ธํฐ๋ท ํ๊ฒฝ์์ ์ ์ํ๋ฉด, ์ฌ์ฉ์๊ฐ ๊ธฐ๋ค๋ฆฌ์ง ๋ชปํ๊ณ ํ๋ฉด์ ๋น ์ ธ๋๊ฐ ์ ์์ผ๋ฏ๋ก ํ์ธํด์ผ ํจ : ์ด๊ธฐ๊ฐ์ ๋ง์ถฐ์!
import { useEffect, useState } from "react";
export default function useFetch(url) {
const [data, setData] = useState([]);
useEffect(() => {
fetch(url)
.then(res => {
return res.json();
})
.then(data => {
setData(data);
});
}, [url]);
return data;
}
const [data, setData] =useState([]);์ด๊ธฐ๊ฐ์ด ๋น ๋ฐฐ์ด
๋ฐ๋ผ์, ์ด๊ธฐ๊ฐ์ด๋ฉด, ๋ก๋ฉ์ค์ด๋ผ๋ ํ๋ฉด์ ๋์์ฃผ๊ธฐ
import { Link } from "react-router-dom";
import useFetch from "../hooks/useFetch";
export default function DayList() {
const days = useFetch("http://localhost:3001/days");
if(days.length === 0 ){
return <span>Loading....</span>
}
return (
<ul className="list_day">
{days.map(day => (
<li key={day.id}>
<Link to={`/day/${day.day}`}>Day {day.day}</Link>
</li>
))}
</ul>
);
}
import { useParams } from "react-router-dom";
import useFetch from "../hooks/useFetch";
import Word from "./Word";
export default function Day() {
const { day } = useParams();
const words = useFetch(`http://localhost:3001/words?day=${day}`);
return (
<>
<h2>Day {day}</h2>
{words.length === 0 && <span>Loading</span>}
<table>
<tbody>
{words.map(word => (
<Word word={word} key={word.id} />
))}
</tbody>
</table>
</>
);
}
import React, { useState } 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();
const [isLoading, setIsLoading] = useState(false);
//isLoading์ด false ์ผ ๋๋ง onSubmitํจ์ ์คํ
function onSubmit(e) {
e.preventDefault();
//isLoading ์ด false์ด๋ฉด ํจ์ ์คํ
if (!isLoading) {
setIsLoading(true);
//์คํ๋๋ฉด true๋ก ์ ํ
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}`);
setIsLoading(false); //๋ชจ๋ ์์
์ด ์๋ฃ๋์์ผ๋ฏ๋ก
}
});
}
}
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>
style=
{{
opacity: isLoading ? 0.2 : 1,
}}
<button>{isLoading ? "Saving..." : " ์ ์ฅ"}</button>
//isLoading์ด true(Loading ์ค) ์ผ๋๋, ๋ฒํผ์ Saving์ผ๋ก ๋ฐ๊ฟ์ค
</form>
);
}
์ฝ๋ฉ์๋ง React ๊ฐ์ ๋!
์ด์ , ๋ณต์ต!! + ์ง์ ์ฒ์๋ถํฐ ๋ค์ ๋ง๋ค์ด๋ณด๊ธฐ!!
React์ ๋ํ ๊ฐ๋ ์ ๋ค์ ํ๋ฒ ํ๋ ๋ฐ ์ ๋ง ๋์์ด ๋ ๊ฒ ๊ฐ๋ค.
์งง๊ฒ๋ง ๋งํ์๋ฉด ์ง์ง UX, UI์ ๊ตฌํ์ ํ๋ก ํธ์๋๊ฐ ์ ๋๋ก ๋งก๊ณ ์๊ตฌ๋๋ฅผ ์ง์ ์ ์ผ๋ก ๋๋ ์ ์๊ฒ ๋ ๊ฐ์์๊ณ , ๋๋ฌด ์ฌ๋ฏธ์์๋ค. ๋ญ๊ฐ ์๊ธธ์ด ํธ์ธ ๋๋์ด๋ผ...!
์ด์ ๋๋๊ณ ํ ์ผ...
- ์ฒ์๋ถํฐ ๋ค์ ๊ตฌํํด๋ณด๊ธฐ
- Github์ ์ ๋ก๋
- ๊ณผ์ : Day๋ฅผ ์๋ชป ๋ง๋ค์์ ๋, Day๋ฅผ ์ญ์ + ๊ฐ Day์์ ํ์ดํ๋ฅผ ์ด์ฉํ์ฌ ๋ค๋ฅธ Day ํ์ด์ง๋ก ์ด๋ํ๋๋ก
- ์์ธํ ๋ฆฌ๋ทฐ ์์ฑ