๐ ์๋ก
React Query๋ ํด๋ผ์ด์ธํธ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์๋ฒ์ ํต์ ํ ๋ ๋ฐ์ดํฐ ํ์นญ, ์บ์ฑ, ๋๊ธฐํ ๋ฐ ์ํ ๊ด๋ฆฌ๋ฅผ ๋จ์ํํ๊ธฐ ์ํด ๊ฐ๋ฐ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
์ด๋ฒ ํฌ์คํ ์์๋ **“์ React Query๋ฅผ ์ฌ์ฉํ๋๊ฐ?”**๋ผ๋ ์ง๋ฌธ์ ์์์ผ๋ก, ๊ธฐ์กด ๋ฐฉ์๊ณผ React Query ๋ฐฉ์์ ์ฝ๋๋ฅผ ๋น๊ตํ์ฌ ๊ฐ์ ๋ ๋ถ๋ถ์ ์ค๋ช ํ๊ฒ ์ต๋๋ค.
์๋์ ์ฝ๋๋, ํจ์ ์์นด์ด๋ธ ํ๋ก์ ํธ์์์ ์๋ฒ ํต์ ์์ ๊ธฐ์กด์๋ React Query๋ฅผ ์ฐ์ง ์๊ณ ๊ทธ๋ฅ axios๋ก ์ฒ๋ฆฌ๋ฅผ ํ๋ ๋ฒ๊ณผ,
React Query๋ฅผ ์จ์ ์๋ฒ์์ ํต์ ์ ํ ๋์ ์ฐจ์ด์ ์ ๊ณต๋ถํ ๋ด์ฉ์ ๋๋ค.
๋ชจ๋ ์ฝ๋๊ฐ ์๋ ํต์ฌ ์ฝ๋๋ง ๋ฃ๊ฒ ์ต๋๋ค ( ๋ณด์๋ ๋ถ์ ๊ฐ๋ ์ฑ์ ์ํ์ฌ,,)
1๏ธโฃ ๊ธฐ์กด ์ฝ๋ ๋ฆฌ๋ทฐ
๐ง ๊ธฐ์กด ์ฝ๋ (Axios + useEffect ๋ฐฉ์)
์๋ฒ๋ JSON ์๋ฒ๋ฅผ ํ์ฉํ์ต๋๋ค.
import axios from 'axios';
import { useEffect, useState } from 'react';
const fetchData = () => {
const [shoes, setShoes] = useState([]);
const url = `https://codingapple1.github.io/shop/data2.json`;
useEffect(() => {
axios
.get(url)
.then((response) => setShoes(response.data))
.catch((error) => console.error('Error fetching data:', error));
}, []);
return <ShoeList shoes={shoes} />;
};
๐ ๊ธฐ์กด ์ฝ๋์ ๋ฌธ์ ์
1. ์ค๋ณต ์ฝ๋ ๋ฐ์: ๋ฐ์ดํฐ fetching ์ฝ๋๊ฐ ์ฌ๋ฌ ์ปดํฌ๋ํธ์ ๋ฐ๋ณต๋ ๊ฐ๋ฅ์ฑ์ด ํผ.
2. ์๋ฌ ํธ๋ค๋ง ๋ถ์กฑ: ๋คํธ์ํฌ ์ฅ์ ๋ API ์คํจ ์ ์ฌ์๋๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ์ถ๊ฐ ์ฝ๋๊ฐ ํ์.
3. ์บ์ฑ ๋ฏธ์ง์: ๋์ผํ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ์์ฒญํ ๋, ๋งค๋ฒ ์๋ฒ์์ ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ด.
4. ๋ฆฌํจ์น ๋ฐ ์ํ ๋๊ธฐํ ๋ฌธ์ : ์ ๋ฐ์ดํฐ๊ฐ ํ์ํ ์์ ์ ์ง์ ๊ด๋ฆฌํด์ผ ํจ.
2๏ธโฃ ๊ฐ์ ๋ React Query ์ฝ๋
๐ง React Query ์ฌ์ฉ ์ฝ๋
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import ShoeList from './Components/ShoeList';
const fetchShoes = async () => {
const response = await axios.get(`https://codingapple1.github.io/shop/data2.json`);
return response.data; // ์๋ฒ์์ ๋ฐ์์จ ๋ฐ์ดํฐ ๋ฐํ
};
const ShoeListWithReactQuery = () => {
// useQuery ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ
const queryResult = useQuery(['shoes'], fetchShoes, {
staleTime: 1000 * 60 * 5, // 5๋ถ ๋์ ๋ฐ์ดํฐ๊ฐ ์ ์ ํ๋ค๊ณ ๊ฐ์ฃผ
cacheTime: 1000 * 60 * 30, // 30๋ถ ๋์ ์บ์์ ์ ์ง
});
// ์ํ์ ๋ฐ๋ผ UI๋ฅผ ๋ค๋ฅด๊ฒ ๋ณด์ฌ์ฃผ๊ธฐ
if (queryResult.isLoading) {
return <p>Loading...</p>; // ๋ก๋ฉ ์ค์ผ ๋
}
if (queryResult.isError) {
return <p>Error fetching shoes.</p>; // ์๋ฌ ๋ฐ์ ์
}
const shoes = queryResult.data || []; // ๋ฐ์ดํฐ๊ฐ ์์ ๊ฒฝ์ฐ ๋น ๋ฐฐ์ด๋ก ์ฒ๋ฆฌ
return (
<div>
<button onClick={() => queryResult.refetch()}>์๋ก๊ณ ์นจ</button>
<ShoeList shoes={shoes} /> {/* shoes ๋ฐ์ดํฐ๋ฅผ ShoeList ์ปดํฌ๋ํธ์ ์ ๋ฌ */}
</div>
);
};
export default ShoeListWithReactQuery;
๐ ์ฝ๋ ๋ถ์
์ฒ์ ๋ณด๋ฉด ๋๋ถ๋ถ ์ ๊ฒ ๋ญ์ง ์ถ์ ๊ฒ๋๋ค.
1. queryResult๋ผ๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํฉ๋๋ค.
const queryResult๋ useQuery ํจ์๊ฐ ๋ฐํํ๋ ๊ฐ์ฒด์ ๋๋ค.
ํด๋น ๊ฐ์ฒด๋ ํฌ๊ฒ 4๊ฐ์ง ๊ธฐ๋ฅ์ด ์์ต๋๋ค.
• queryResult.data → ์๋ฒ์์ ๋ฐ์์จ ๋ฐ์ดํฐ
• queryResult.isLoading → ๋ก๋ฉ ์ค ์ฌ๋ถ
• queryResult.isError → ์๋ฌ ์ฌ๋ถ
• queryResult.refetch() → ๋ฐ์ดํฐ ์๋ก๊ณ ์นจ
์์ 3๊ฐ์ง ๊ธฐ๋ฅ์ Boolean ํํ๋ฅผ return ํ๋ ๋๊ฐ ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ ํตํ์ฌ ๋ก๋ฉ ์ค์ธ์ง, ์๋ฒ์์ ํต์ ์ด ์คํจํ๋์ง, ์ฑ๊ณตํ๋์ง๋ฅผ ํ์ ํฉ๋๋ค.
2. ์กฐ๊ฑด๋ถ ๋ ๋๋ง
• ๋ก๋ฉ ์ค์ผ ๋ "Loading..." ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ค๋๋ค.
• ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋ "Error fetching shoes." ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ค๋๋ค.
• ์ ์์ ์ธ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์์ ๊ฒฝ์ฐ ShoeList ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํฉ๋๋ค.
//{queryResult.data && '์ฑ๊ณต์'}
//{queryResult.isLoading && '๋ก๋ฉ์ค์'}
//{queryResult.error && '์ฑ๊ณต์๋'} ๋ญ ์ด๋ฐ์์ผ๋ก ์จ๋ ๊ด์ฐฎ๊ฒ ๋ค์.
3๏ธโฃ React Query ์ฌ์ฉ ์ ์ด์
๐ ์ฃผ์ ์ฅ์
1. ์บ์ฑ ๋ฐ ์ฌ์ฌ์ฉ์ฑ: ๋์ผํ ์ฟผ๋ฆฌ๋ฅผ ์ค๋ณต ์์ฒญํ์ง ์๊ณ ์บ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ์ฌ ๋คํธ์ํฌ ์์ฒญ์ ์ต์ํํฉ๋๋ค.
2. ์๋ ๋ฆฌํจ์น: ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ์ ๋ฐ์ดํฐ๊ฐ ํ์ํ ๋ ์๋์ผ๋ก ๋ฆฌํจ์น๋ฅผ ์ํํฉ๋๋ค.
3. ์๋ฌ ๋ฐ ๋ก๋ฉ ์ํ ๊ด๋ฆฌ: isLoading, isError ๋ฑ ์ํ๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํ ๋ก์ง์ด ๊ฐ๋จํด์ง๋๋ค.
4. ์๋ ์ฌ์๋: ๋คํธ์ํฌ ์ค๋ฅ ๋ฐ์ ์ ์ผ์ ํ์๊น์ง ์๋์ผ๋ก ์ฌ์๋ํฉ๋๋ค.
5. ์ฝ๋ ๊ฐ์ํ: useQuery ํ ํ๋๋ก useState, useEffect, axios ์ฝ๋๋ฅผ ๋์ฒดํ ์ ์์ต๋๋ค.
๊ธฐ์กด ๋ฐฉ์ (useEffect + Axios)๊ณผ์ ๋น๊ต
๊ธฐ์กด ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ fetchํ ๋๋ useState์ useEffect๋ฅผ ์ฌ์ฉํ์ฌ ๋ก์ปฌ ์ํ๋ฅผ ๊ด๋ฆฌํด์ผ ํ์ต๋๋ค. ์๋ ์ฝ๋๋ ์ ๋ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ์ปดํฌ๋ํธ์ ๋๋ค.
function App() {
const [shoes, setShoes] = useState([]);
useEffect(() => {
axios
.get('https://codingapple1.github.io/shop/data1.json')
.then((response) => setShoes(response.data))
.catch((error) => console.log(error));
}, []);
return <ShoeList shoes={shoes} />;
}
• ๋ฐ์ดํฐ ๋ก๋ฉ ์ํ์ ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ์ํด ์ถ๊ฐ ์ฝ๋๊ฐ ํ์ํฉ๋๋ค.
• ๋์ผํ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ์์ฒญํ ๋ ๋งค๋ฒ ์๋ฒ ์์ฒญ์ด ๋ฐ์ํฉ๋๋ค.
• ๋คํธ์ํฌ ์คํจ ์ ์ถ๊ฐ์ ์ผ๋ก ์ฌ์๋ ๋ก์ง์ ์์ฑํด์ผ ํฉ๋๋ค.
๊ฐ์ ๋ React Query ๋ฐฉ์
React Query๋ฅผ ์ฌ์ฉํ๋ฉด useQuery๋ฅผ ํตํด ๋น๋๊ธฐ ๋ฐ์ดํฐ fetching์ ๊ฐ๋จํ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
์ด๋ฒ์ ์ข ๋ ํธํ๋ฐฉ์์ธ ๊ตฌ์กฐ๋ถํดํ ๋น์ ํ์ฌ queryResult ๊ฐ์ฒด๋ฅผ ์ข ๋ถ๋ฆฌํด์ ์จ๋ณด๊ฒ ์ต๋๋ค.
const fetchShoes = async () => {
const { data } = await axios.get('https://codingapple1.github.io/shop/data1.json');
return data;
};
function App() {
const { data: shoes = [], isLoading, isError } = useQuery(['shoes'], fetchShoes);
if (isLoading) return <p>๋ก๋ฉ ์ค...</p>;
if (isError) return <p>๋ฐ์ดํฐ ๋ก๋ ์คํจ</p>;
return <ShoeList shoes={shoes} />;
}
ํฌ๊ฒ 3๊ฐ์ง ์ฅ์ ์ด ์๋๋ฐ ์๋์ ๊ฐ์ต๋๋ค.
• isLoading, isError๋ก ๋ก๋ฉ ๋ฐ ์๋ฌ ์ํ๋ฅผ ๊ฐ๋จํ๊ฒ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
• ์๋์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฆฌํจ์นํฉ๋๋ค.
• ๋์ผํ ์์ฒญ์ ๋ํ ์บ์ฑ์ ์ง์ํ๋ฏ๋ก ๋คํธ์ํฌ ๋ถ๋ด์ด ์ค์ด๋ญ๋๋ค.
๊ฒฐ๋ก ๋ด๋ฆฌ๊ธฐ ์ ..๊ตฌ์กฐ ๋ถํด ํ ๋น์ด ๋ฌด์์ธ๊ฐ?
์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฃผ ์ค์ํ ๋ฌธ๋ฒ์ ๋๋ค.
ํ๋ฒ ์ง๊ณ ๋์ด๊ฐ๋ณด๊ฒ ์ต๋๋ค !!!
const user = {
name: 'John Doe',
age: 30,
job: 'Developer',
};
user๋ผ๋ ๊ฐ์ฒด๊ฐ ์๋ค๊ณ ๊ฐ์ ํฉ์๋ค.
name,age,job์ด๋ผ๋ ์์ฑ์ด ๋ค์ด์๊ณ ์ฐ๋ฆฐ ์ด ๊ฐ์ ์ ๊ทผํ๋ ค๋ฉด ์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์ง๊ณ ๋ ํฉ๋๋ค.
const name = user.name; // 'John Doe'
const age = user.age; // 30
const job = user.job; // 'Developer'
๊ตฌ์กฐ ๋ถํด ํ ๋น ์ฌ์ฉํ๊ธฐ ( ๊ฐ์ฒด์ ์์ฑ์ ๋ฐ์ผ๋ก ๋นผ์!)
๋ง ๊ทธ๋๋ก ๊ฐ์ฒด์ ์์ฑ๋ช ์ {์์ฑ๋ช 1, ์์ฑ๋ช 2, ์์ฑ๋ช 3} = ๊ฐ์ฒด์ด๋ฆ ์ผ๋ก ๋ป ๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋์ ์์ฑ๋ช 1,2,3์ ๊ฐ์ฒด์ ์ ๊ทผ๋ฒ์ธ dot ์ฐ์ฐ์๋ฅผ ์ฐ์ง ์๊ณ ๋ฐ๋ก ์์ฑ์ ๊ฐ์ ์ ๊ทผ์ ํ๋ ๊ฒ์ ๋๋ค.
const { name, age, job } = user;
console.log(name); // 'John Doe'
console.log(age); // 30
console.log(job); // 'Developer'
๊ธฐ๋ณธ๊ฐ ์ค์ ํ๊ธฐ
์ด? ๊ทผ๋ฐ ์์๋ data : shoes = [] ๋ฏ์ ๋ถ๋ถ์ด ๋์์ต๋๋ค!
const fetchShoes = async () => {
const { data } = await axios.get('https://codingapple1.github.io/shop/data1.json');
return data;
};
function App() {
const { data: shoes = [], isLoading, isError } = useQuery(['shoes'], fetchShoes);
if (isLoading) return <p>๋ก๋ฉ ์ค...</p>;
if (isError) return <p>๋ฐ์ดํฐ ๋ก๋ ์คํจ</p>;
return <ShoeList shoes={shoes} />;
}
์ด ๋ถ๋ถ์ ์๋ ์์ฑ์ ๊ฐ์ ๊ตฌ์กฐ๋ถํดํ ๋น ํ๊ณ ์ถ์๋, ๊ฐ์ ๋ก ๊ฐ์ ์ค์ ํด์ฃผ๋ ๊ฒ์ด๋ผ๊ณ ๋ณด๋ฉด ๋ฉ๋๋ค.
์ฌ์ด์์
const { name, age, job } = user;
console.log(name); // 'John Doe'
console.log(age); // 30
console.log(job); // 'Developer'
const { name, gender = 'unknown' } = user;
console.log(name); // 'John Doe'
console.log(gender); // 'unknown' (๊ฐ์ฒด์ gender ์์ฑ์ด ์๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋ ๊ฐ์ด ์ถ๋ ฅ๋จ)
์ด์ ๊ทธ๋ผ gender๋ผ๋ ์ฐ๋ฆฌ๊ฐ ๊ฐ์ ๋ก ๋ง๋ ์์ฑ์ ๋ํดํธ ๊ฐ์ธ Unknown์ด ์ถ๋ ฅ๋ฉ๋๋ค.
๊ทธ๋ผ gender๋ผ๋ ์์ฑ์ user ๊ฐ์ฒด์ ์ถ๊ฐ๋์๋๊ฐ?
์ฆ, ๊ตฌ์กฐ ๋ถํด ํ ๋น์ผ๋ก ๊ฐ์ฒด ์์ฒด์ ๋ณํ๊ฐ ์ผ์ด๋๋๊ฐ?
๊ทธ๊ฒ์ ์๋๋๋ค! (๋คํ์ ๋๋ค)
const user = {
name: 'John Doe',
age: 30,
job: 'Developer',
};
const { name, gender = 'unknown' } = user;
console.log(name); // 'John Doe'
console.log(gender); // 'unknown'
console.log(user); // { name: 'John Doe', age: 30, job: 'Developer' }
• user ๊ฐ์ฒด๋ ์ฌ์ ํ { name: 'John Doe', age: 30, job: 'Developer' }์ ๋๋ค.
• gender = 'unknown'์ ๊ตฌ์กฐ ๋ถํด ํ ๋น ์ gender๋ผ๋ ๋ณ์๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ด๊ธฐํํ ๊ฒ์ผ ๋ฟ user ๊ฐ์ฒด์ ์์ฑ์ด ์ถ๊ฐ๋ ๊ฑด ์๋๋๋ค.
์ต์ง๋ก ์์ฑ๋ช (๋ณ์๋ช )์ ์ถ๊ฐํด์ ๊ฐ์ ์ป๊ฒ ๋ค ์ ๋์ ์ทจ์ง์ด์ง, ๊ฐ์ฒด ์์ฒด์ ๋ถ๋ณ์ฑ์ ์ ์ง๋ฉ๋๋ค!
5. useQuery์ ๊ตฌ์กฐ ๋ถํด ํ ๋น ์์
๊ทธ๋ผ ์๊น useQuery๋ฅผ ์๋ก ๋์๊ฐ์๋ค. ๋ค์๊ณผ ๊ฐ์ queryResult ๊ฐ์ฒด๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค.
์๋ฒ์์ ์ด๋ฐ JSON ํํ๋ฅผ ํ๋ ๋ฐ์์๋ค๊ณ ๊ฐ์ ํ๋ฉด
const queryResult = {
data: [{ id: 1, title: 'Shoe 1' }],
isLoading: false,
isError: false,
refetch: () => console.log('๋ค์ ๊ฐ์ ธ์ค๊ธฐ!'),
};
๊ตฌ์กฐ ๋ถํด ํ ๋น์ผ๋ก queryResult์ ๊ฐ์ ๊บผ๋ด๊ธฐ
๋งจ๋ dot ์ฐ์ฐ์๋ก ์ ๊ทผํ๋ฉด props๋ณด๋ด๊ธฐ๋ ๋ถํธํ๊ณ ๊ท์ฐฎ๋ค! ์ถ์๋ ์ฐ๋๊ฒ๋๋ค.
์ด์ ์ฏค ๊ฐ์ด ์ค์ค๊ฒ๋๋ค.
queryResult๋ผ๋ ๊ฐ์ฒด์ ์์ฑ๋ค์ queryResult.data ๊ฐ ์๋ data!๋ก๋ง ์ฐ๊ณ ์ถ์๊ฒ๋๋ค.
const { data, isLoading, isError } = queryResult;
console.log(data); // [{ id: 1, title: 'Shoe 1' }]
console.log(isLoading); // false
console.log(isError); // false
๋ณ์ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ & ๊ธฐ๋ณธ๊ฐ ์ค์
์ด์ ๋ง์ง๋ง ์ดํด๋จ๊ณ์ ๋๋ค. -> ๊ธฐ๋ณธ๊ฐ ์ค์ (๊ฐ์ ๋ณ์๋ช ๋ฐ๊พธ๊ธฐ)
์ด๋ฐ์์ผ๋ก ๋ง์ ๋๋ค!
const { data: shoes = [], isLoading, isError } = queryResult;
console.log(shoes); // [{ id: 1, title: 'Shoe 1' }]
console.log(isLoading); // false
console.log(isError); // false
shoes๋ [ ] ( ๋น ๋ฐฐ์ด)๋ก ์ด๊ธฐํํ๋๋ฐ ์ ์ฝ์์ ์ฐ์ผ๋ฉด ์ ๊ฐ์ด ๋์ค๋๊ฐ?
์ฐ๋ฆฐ ๋ถ๋ช ๋น ๋ฐฐ์ด๋ก ๊ฐ์ ๊ฐ์ ํ ํ๊ฑฐ ์๋๊ฐ?
๋งค์ฐ ์ข์ ์ง๋ฌธ์ด๊ณ ํ์๋ ๊ทธ๋ ๊ฒ ์๊ฐํ์ต๋๋ค. (ํท๊ฐ๋ฆด์ ์๋ ๋ถ๋ถ์ ๋๋ค)
์ฝ๋๋ฅผ ๋ค์ ์ดํด๋ด ์๋ค.
const { data: shoes = [], isLoading, isError } = queryResult;
shoes = []๋ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ํ ๊ฒ์ ๋๋ค. ๊ธฐ๋ณธ๊ฐ์ queryResult.data๊ฐ undefined ๋๋ null์ผ ๋๋ง ์ ์ฉ๋ฉ๋๋ค. ์ฆ, queryResult.data๊ฐ undefined์ผ ๋๋ง ๋น ๋ฐฐ์ด []๋ก ํ ๋น๋ฉ๋๋ค.
์ฝ๋ ๋์ ์๋ฆฌ
• queryResult.data๊ฐ ์กด์ฌํ๋ฉด shoes์ queryResult.data์ ์ค์ ๊ฐ์ด ํ ๋น๋ฉ๋๋ค.
• queryResult.data๊ฐ undefined์ผ ๊ฒฝ์ฐ shoes์ ๊ธฐ๋ณธ๊ฐ []๊ฐ ํ ๋น๋ฉ๋๋ค.
์ฝ๊ฒ ๋งํ๋ฉด ๋ณ์๋ฅผ ์ด๊ธฐํ ํ๊ฒ์ด์ง ๊ฐ์ ๋ก ๋ ๋ฌด์กฐ๊ฑด ๋น ๊ฐ์ด์ผ๊ฐ ์๋๋ ๊ฒ์ ๋๋ค.
๊ธฐ๋ณธ๊ฐ์ด ์ ์ฉ๋๋ ๊ฒฝ์ฐ
const queryResult = {
data: undefined, // ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค์ง ๋ชปํ์ ๊ฒฝ์ฐ
isLoading: false,
isError: true,
};
const { data: shoes = [], isLoading, isError } = queryResult;
console.log(shoes); // [] (๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋น ๋ฐฐ์ด ํ ๋น)
console.log(isLoading); // false
console.log(isError); // true
์์ ๊ฒฝ์ฐ queryResult ๊ฐ์ฒด์ data ์์ฑ์ ๊ฐ์ด undefined์ ๋๋ค.
๋ฐ๋ผ์ data๋ผ๋ ์์ฑ์ shoes๋ก ๋ณ์๋ช ์ ๊ฐ์ ํํ๊ณ ํด๋น ๊ฐ์ [ ] ๋ก ์ด๊ธฐํ ํ์๋๋
์๋ก ๋ฐ๋ shoes ๋ณ์๋ช ์ undefined๋ฅผ ์ค์๋ ์์ผ๋ [ ] ๋ก ํ ๋น๋๊ฒ ์ต๋๋ค.
์ ๋ฆฌํ๋ฉด
shoes = []๋ ๊ธฐ๋ณธ๊ฐ ์ค์ ์ ๋๋ค.
๊ธฐ๋ณธ๊ฐ์ queryResult.data๊ฐ undefined์ผ ๋๋ง ์ ์ฉ๋๊ธฐ ๋๋ฌธ์,
queryResult.data์ ์ค์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ๋์ ํด๋น ๋ฐ์ดํฐ๊ฐ ํ ๋น๋ฉ๋๋ค.
๋ฐ๋ผ์ queryResult.data์ ๊ฐ์ด ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ๋ฌด์๋๋ ๊ฒ์ด ์ ์์ ์ธ ๋์์ ๋๋ค.
• ๋ง์ฝ queryResult.data์ ์ค์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด, shoes์๋ ํด๋น ๋ฐ์ดํฐ๊ฐ ํ ๋น๋ฉ๋๋ค.
๊ธฐ๋ณธ๊ฐ ์ค์ ์ ๊ฐ์ด ์์๋ (null or undefined)์ผ๋๋ฅผ ๋๋นํ๋ ๊ฒ์ ๋๋ค.
๋ง์ง๋ง ๊ฒฐ๋ก
• React Query๋ ์๋ฒ ์ํ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ๋์์ค๋๋ค.
• ๋ฐ์ดํฐ fetching ๋ก์ง์ด ์ปดํฌ๋ํธ ๊ฐ์ ์ค๋ณต๋์ง ์์ผ๋ฉฐ, ์๋ฌ ํธ๋ค๋ง ๋ฐ ๋ก๋ฉ ์ํ ๊ด๋ฆฌ๊ฐ ๋งค์ฐ ๊ฐ๋จํด์ง๋๋ค.
• ์บ์๋ฅผ ํ์ฉํ์ฌ ๋ถํ์ํ ๋คํธ์ํฌ ์์ฒญ์ ์ค์ด๊ณ ์ฑ์ ์ฑ๋ฅ์ ํฅ์์ํต๋๋ค.
๋ฐ๋ผ์ ์๋ฒ ํต์ ์ด ๋น๋ฒํ๊ฑฐ๋ ๋ง์ ๋น๋๊ธฐ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํด์ผ ํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ด๋ผ๋ฉด, React Query๋ ํ์์ ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ๊ณ ํ ์ ์์ต๋๋ค.