
isLoding ์ํ(state) ๋ง๋ค๊ณ , ์ด๊ธฐ๊ฐ์ true๋ก ์ค์ ํ๊ธฐ.const [isLoding, setIsLoading] = useState(true);
useEffect ๋ด๋ถ์ fetchMeals ํจ์์์ ๋ชจ๋ ๋ฐ์ดํฐ ๋ก๋ฉ์ด ๋๋๋ฉด setIsLoading ์ํ ์
๋ฐ์ดํธ ํจ์๋ฅผ ์ฌ์ฉํด์ false๋ก ์
๋ฐ์ดํธํ๊ธฐ.useEffect(() => {
const fetchMeals = async () => {
const response = await fetch(
"https://react-http2-xxxxxxxxx.firebaseio.com/meals.json"
);
const responseData = await response.json();
const loadedMeals = [];
for (const key in responseData) {
loadedMeals.push();
}
setMeals(loadedMeals);
setIsLoading(false);
};
fetchMeals();
}, []);
isLoding ์ํ ๊ฐ์ ์ด์ฉํ์ฌ ๋ก๋ฉ ์ค์์ ์๋ฆฌ๋ ๋งํฌ์
์ฝ๋๋ฅผ mealsList ์ปดํฌ๋ํธ ์์ ์์ฑ.if (isLoding) {
return (
<section className={classes.MealsLoading}>
<p>Loding...</p>
</section>
);
}
const mealsList = meals.map((item) => (
<MealItem
key={item.id}
id={item.id}
name={item.name}
description={item.description}
price={item.price}
/>
));
<section> ์ ์คํ์ผ์ ์
ํ๋ค..MealsLoading {
text-align: center;
color: white;
}

useEffect ๋ก์ง์ด๋ค.useEffect(() => {
const fetchMeals = async () => {
const response = await fetch(
"https://react-http2-xxxxxxxxx.firebaseio.com/meals.json"
);
const responseData = await response.json();
const loadedMeals = [];
for (const key in responseData) {
loadedMeals.push();
}
setMeals(loadedMeals);
setIsLoading(false);
};
fetchMeals();
}, []);
.json์ ์ฃผ์์์ ์ญ์ ํ๊ณ ์ ์ฅํ ๋ค ํ์ธํด๋ณด๋ฉด,useEffect(() => {
const fetchMeals = async () => {
const response = await fetch(
"https://react-http2-xxxxxxxxx.firebaseio.com/meals"
);
const responseData = await response.json();
const loadedMeals = [];
for (const key in responseData) {
loadedMeals.push();
}
setMeals(loadedMeals);
setIsLoading(false);
};
fetchMeals();
}, []);

const [httpError, setHttpError] = useState(null);
httpError ์ํ๋ฅผ ์์ฑํ๊ณ , ์ด๊ธฐ ๊ฐ์ ๋น์๋๊ฑฐ๋ ์์ null๋ก ์ค์ ํด์ ์ฒ์์ ๊ฐ์ ๊ฐ์ง ์๋๋ค๋ ๋ชฉ์ ์ ๋ ๋ถ๋ช
ํ ํด๋๋ ๊ฒ๋ ์ข์ ๊ฒ์ด๋ค. ์ด์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋๋ฐ ์คํจํ์ ๋์ ์ค๋ฅ๋ฅผ ์ค์ ํ์.useEffect(() => {
const fetchMeals = async () => {
const response = await fetch(
"https://react-http2-xxxxxxxxx.firebaseio.com/meals"
);
if (!response.ok) {
}
const responseData = await response.json();
const loadedMeals = [];
for (const key in responseData) {
loadedMeals.push();
}
setMeals(loadedMeals);
setIsLoading(false);
};
fetchMeals();
}, []);
if ๋ฌธ์ ์์ฑํ๊ณ ๋ด๋ถ์ !response.ok ๋ผ๋ ์กฐ๊ฑด์ ๋ฃ๋๋ค. ok๊ฐ truthy ์ธ ๊ฒฝ์ฐ๋ฅผ ๋งํ๊ธฐ ๋๋ฌธ์ ๋๋ํ๋ฅผ ๋ฃ์ด์ falsy๋ก ๋ง๋ค๊ณ , response๊ฐ ok ๋์ง ์์์ ๋ ์ฆ, ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋๋ฐ ์คํจํ์ ๋๋ฅผ ๊ฐ์ ํ๊ณ ๋ก์ง์ ์์ฑํ๋ค.if (!response.ok) {
throw new Error("Something went wrong!");
}
throw new Error()๋ ์ค์ด ์คํ๋์ง ์๋๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
๋ง์ฝ !response.ok๋ผ๋ฉด(๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋๋ฐ ์คํจํ๋ค๋ฉด) throw new Error()๋ฅผ ์์ฑํด์ ์๋ก์ด ์ค๋ฅ๋ฅผ ์์ฑํ๊ณ ์ด ์๋ฌ์ ์ผ๋ฐ์ ์ธ ์ค๋ฅ ๋ฉ์ธ์ง("Something went wrong!")๋ฅผ ํ ๋นํ๋ค. ์ด์ ์ด ๋ง๋ค์ด์ง ์๋ก์ด error๋ฅผ ๊ฐ์ง๊ณ ํธ๋ค๋ง์ ํด์ผํ๋ค.
์ฐ๋ฆฌ๋ ์ฌ๊ธฐ์ fetch ๋ก์ง์ ๋ฉํ ํ๊ธฐ ์ํด ๋ณ๋์ ํจ์(fetchMeals)๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ํ ๋ ๋ค๋ฅธ ์ฅ์ ์ ์ ์ ์๋ค. fetchMeals ํจ์๋ ์์ฒญ์ ๋ณด๋ผ ๋ ๋ฌธ์ ๊ฐ ์๊ธฐ๋ฉด ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ๋ฐ์์ํฌ ๊ฒ์ด๋ค. ๋ฐ๋ผ์ fetchMeals๋ก ์ด๋ํด์ ๋ฌด์ธ๊ฐ๋ฅผ ์ถ๊ฐํด์ผ๋ง ํ๋ค.
useEffect(() => {
const fetchMeals = async () => {
const response = await fetch(
"https://react-http2-xxxxxxxxx.firebaseio.com/meals"
);
if (!response.ok) {
throw new Error("Something went wrong!");
}
const responseData = await response.json();
const loadedMeals = [];
for (const key in responseData) {
loadedMeals.push();
}
setMeals(loadedMeals);
setIsLoading(false);
};
try {
} catch {}
fetchMeals();
}, []);
fetchMeals() ํจ์๋ฅผ ํธ์ถํ๋ ์ค์ useEffect ์์ ์์ง๋ง fetchMeals ํจ์์ ์ธ๋ถ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๊ฒ์ ์ฐ๋ฆฌ๋ try/catch๋ก ๋ํํ ์ ์๊ฒ ๋๋ค.
try/catch๋ try(์๋ํ๊ธฐ)ํด์ catch(์ก๊ธฐ)ํ๋ค๋ ์๋ฏธ์ด๋ค.
try {
fetchMeals();
} catch {}
try(์๋ํ๊ธฐ) ๋ก์ง ์์์ fetchMeals ํจ์๋ฅผ ํธ์ถํ๊ณ , fetchMeals ํจ์ ๋ด๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค๋ฉด catch(์ก๊ธฐ) ๋ธ๋ก ์์์ ๋ฌด์ธ๊ฐ๋ฅผ ์ฒ๋ฆฌํ๋๋ก ํ๋ค.try {
fetchMeals();
} catch {
setIsLoading(false);
}
catch ๋ธ๋ก ์์์ setIsLoading(false) ๋ก ์ฒ๋ฆฌํ๋ค. fetchMeals() ํจ์๋ฅผ try(์๋) ํ๋๋ฐ, ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค๋ฉด ๋ก๋ฉ์ ๋ฉ์ถ๊ณ (setIsLoading(false)), ๋ญ๊ฐ์ ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ์ฒ๋ฆฌํ๋๋ก ํด์ผ๋๊ธฐ ๋๋ฌธ์ด๋ค.try {
fetchMeals();
} catch (error) {
setIsLoading(false);
setHttpError(error.message);
}
setHttpError๋ฅผ ํธ์ถํ๊ณ ์ฐ๋ฆฌ๊ฐ catch(์ก์)ํ ์ค๋ฅ ๋ฉ์ธ์ง(error.message)๋ฅผ ๋ณด์ฌ์ฃผ๋๋ก ํ๋ค. ๊ทธ๋ผ try/catch๋ฅผ ์ฌ์ฉํ์ฌ ์ด error์ ์ ๊ทผํ ์ ์๊ฒ ๋๋ฉฐ httpError ์ํ(state)์ ์ฐ๋ฆฌ๊ฐ ์ด๋ฅผ ํตํด์ ์ก๊ฒ ๋ error์ ๋ํด ์ค์ ํ ์ ์๊ฒ ๋๋ ๊ฒ์ด๋ค. ๋ ์ ํํ๊ฒ๋ error.message์ ์ ๊ทผํ๊ณ ์ค์ ํ ์ ์๋ค๋ ๋ป์ด๋ค.if (!response.ok) {
throw new Error("Something went wrong!");
}
catch๋ฅผ ํตํด์ error ๊ฐ์ฒด๋ฅผ ์ป๊ฒ ๋์๊ณ , ์ด error ๊ฐ์ฒด๋ ๋ํดํธ๋ก message ์์ฑ์ ๊ฐ์ง๊ฒ ๋๋ค. throw new Error()๋ฅผ ํตํด์ error๋ฅผ ์์ฑํ๊ณ ์์ฑ์์ ๋ฌธ์์ด("Something went wrong!")์ ์
๋ ฅํ๋ฉด ์์ฑ๋ error ๊ฐ์ฒด์ message ์์ฑ์ ํด๋น ๋ฌธ์์ด์ด ์ ์ฅ๋ ๊ฒ์ด๋ค.if (isLoding) {
return (
<section className={classes.MealsLoading}>
<p>Loding...</p>
</section>
);
}
if (httpError) {
return (
<section>
<p></p>
</section>
);
}
httpError ์ํ๋ฅผ ์ค์ ํด์ผ ํ๋ค. if ๋ฌธ์ ํตํด์ httpError ์ํ ๊ฐ์ด ๋น์์ ธ ์์ง ์๋ค๋ฉด ์ฆ, ์ฐ๋ฆฌ๊ฐ throw new Error()๋ก ์ ์ฅํ "Something went wrong!"๊ฐ httpError ์ํ ์์ ๋ค์ด์๋ค๋ฉด ๋ถ๋ช
์๋ฌ๊ฐ ๋ฐ์ํ์ ๊ฒฝ์ฐ์ผ ๊ฒ์ด๊ณ ์ด์ ๋ํ ๋ก์ง์ ์์ฑํด์ผ ํ ๊ฒ์ด๋ค.if (httpError) {
return (
<section>
<p>{httpError}</p>
</section>
);
}
httpError ์ ์ ์ฅ๋ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋๋ก <p> ํ๊ทธ ์์ httpError ์ํ๋ฅผ ๋ฃ์ด๋๊ณ ,if (httpError) {
return (
<section className={classes.MealsError}>
<p>{httpError}</p>
</section>
);
}
<section> ์ ์คํ์ผ์ ์
ํ๋ค..MealsError {
text-align: center;
color: red;
}
try {
fetchMeals().catch();
} catch {
setIsLoading(false);
}
try/catch๋ฅผ ์ฌ์ฉํ๊ณ ์์ง๋ง fetchMeals ๋ async ํจ์ ๋ผ๋ ์ฌ์ค์ ๊ธฐ์ตํด์ผ ํ ๊ฒ์ด๋ค.fetchMeals ๋ async ํจ์์ด๋ค.useEffect(() => {
const fetchMeals = async () => {
const response = await fetch(
"https://react-http2-xxxxxxxxx.firebaseio.com/meals"
);
if (!response.ok) {
throw new Error("Something went wrong!");
}
const responseData = await response.json();
const loadedMeals = [];
for (const key in responseData) {
loadedMeals.push();
}
setMeals(loadedMeals);
setIsLoading(false);
};
try {
fetchMeals().catch();
} catch {
setIsLoading(false);
}
}, []);
fetchMeals ๋ async ํจ์์ด๋ค. ์ด๊ฒ์ด ๋ฌด์จ ๋ป์ด๋ ํ๋ฉด, ์ธ์ ๋ ์ด ํจ์๋ promise๋ฅผ ๋ฐํํ๋ค๋ ๊ฒ์ด๋ค. promise ๋์ error๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ ๊ทธ ์ค๋ฅ๋ก ์ธํด ํด๋น promise๊ฐ ๊ฑฐ๋ถํ๊ฒ ๋๊ณ , ๋ฐ๋ผ์ ์ฐ๋ฆฌ์ try/catch๋ฅผ ์ฌ์ฉํด์ ๊ทธ๊ฒ์ ๋ํํ ์ ์๊ฒ ๋๋ ๊ฒ์ด๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ๋ฆฌ๋ ์ด๋ป๊ฒ ํด๊ฒฐํด์ผ ํ ๊น? ๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค.try {
fetchMeals().catch(() => {});
} catch {
setIsLoading(false);
setHttpError(error.message);
}
try/catch๋ก ๋ํํ ๊ฒ์ ๋ณ๋์ ํจ์์ ์
๋ ฅํ๋ ๊ฒ์ด๋ค. promise์ catch ๋ฉ์๋์ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค. fetchMeals()๊ฐ promise๋ฅผ ๋ฐํํ๋ฏ๋ก then()์ ์ถ๊ฐํด์ ์ฑ๊ณตํ ์ ์์๋๋ฐ, ์ด๋ promise๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ดํํ์ ๋๋ฅผ ๊ฐ์ ํด์ ์ฌ์ฉํด์ผ ํ๋ค. ์๋ฌดํผ fetchMeals()์ catch ๋ฉ์๋๋ฅผ ์ถ๊ฐํ์ฌ, erorr๋ฅผ ๋ค๋ฃฐ ์ ์๊ฒ ๋์๋ค. ๊ทธ๋ฆฌ๊ณ ์ด error๋ promise ๋ด๋ถ์์ ๋ฐ์ํ๋ค.fetchMeals().catch((error) => {});
try/catch๋ฅผ ์ฌ์ฉํ๋ ๋์ ์ฐ๋ฆฌ๊ฐ ์ป๋ error์ ๋ํด catch()๋ฅผ ์ ์ฉํ ์ ์์ ๊ฒ์ด๋ค.fetchMeals().catch((error) => {
setIsLoading(false);
setHttpError(error.message);
});
try/catch ๋ธ๋ก(catch{}) ์์์ ํธ์ถํ๋ setIsLoading์ setHttpError ์ํ ๋ก์ง๋ catch() ์์ผ๋ก ์ฎ๊ธฐ๋๋ก ํ๋ค.fetchMeals().catch((error) => {
setIsLoading(false);
setHttpError(error.message);
});

๐จ ํด๋น ํฌ์คํ ์ Udemy์ โReact ์๋ฒฝ ๊ฐ์ด๋โ ๊ฐ์๋ฅผ ๋ฒ ์ด์ค๋ก ํ ๊ธฐ๋ก์ ๋๋ค.
โ๐ป ๊ฐ์ git repo ๋ฐ๋ก๊ฐ๊ธฐ