ํฐ์คํ ๋ฆฌ ๋ทฐ
[์ฌ๋๋ณ_Day8] Tanstack Query ์ด๊ธฐ ๋ก๋ฉ ์ฒ๋ฆฌ - initialData
์ฑ._. 2025. 4. 6. 23:43์ด์ ์ํฉ : Tanstack Query ์ ์ฉ ์ค undefined ๋ฐฉ์ง
useEffect๋ก ์ฒ๋ฆฌํ๋ ์ด์ ๋ก์ง์ tanstack์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํด ์ปค์คํ ํ ์ผ๋ก ๋บ๋ค.
์๋ ๋ก์ง์ formatted ๋ก์ง์ ์ ์ธํ, supabase์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ๋ถ๋ถ๋ง ์ปค์คํ ์ผ๋ก ๋บ์ ๋์ ๋ก์ง์ด๋ค.
'use client';
import { Calendar, dateFnsLocalizer } from 'react-big-calendar';
import { format, parse, startOfWeek, getDay } from 'date-fns';
import { enUS } from 'date-fns/locale/en-US';
import { ko } from 'date-fns/locale/ko';
import { useState } from 'react';
import { CalendarEventType, PlansType } from '@/types/plans';
import { useGetCalendarPlans } from '@/hooks/useGetCalendarPlans';
// ๋ก์ผ์ผ(์ง์ญํ) ์ค์
const locales = {
'en-US': enUS, //๋ฏธ๊ตญ-์์ด : ๋ฏธ๊ตญ์ ๋ ์ง
'ko-KR': ko, //ํ๊ตญ-ํ๊ธ : ํ๊ตญ์ ๋ ์ง
};
const localizer = dateFnsLocalizer({
format,
parse,
startOfWeek,
getDay,
locales,
});
const MainCalendar = () => {
const [events, setEvents] = useState<CalendarEventType[]>([]);
const { data: plans, isPending, isError, error } = useGetCalendarPlans();
const fetchEvents = async () => {
const formatted = plans.map(
(plan: PlansType): CalendarEventType => ({
id: plan.plan_id,
title: plan.title,
start: new Date(plan.start_date),
end: new Date(plan.end_date),
})
);
setEvents(formatted);
};
if (isPending) {
return <div>๋ก๋ฉ ์ค์
๋๋ค...</div>;
}
if (isError) {
return <div>์บ๋ฆฐ๋ ์๋ฌ ๋ฐ์ : {error.message}</div>;
}
return (
<div>
<Calendar
localizer={localizer}
events={events} // ์ฌ๊ธฐ์ ์ด๋ฒคํธ ์ถ๊ฐ ๊ฐ๋ฅ
startAccessor='start'
endAccessor='end'
style={{ height: 500 }}
/>
</div>
);
};
export default MainCalendar;
์ด๋, plans์ ๊ฐ์ด undefined๊ฐ ๋ ์๋ ์๋ค๋ eslint์ ๊ฐ์๊ฐ ์์๋ค..!
์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด์ useQuery์ ํ์ ์ ๋ช ์ํจ๊ณผ ํจ๊ป initialData์ ์ถ๊ฐํ์ฌ ์ด๊ธฐ๊ฐ์ ๋น ๋ฐฐ์ด๋ก ์ฃผ์๋ค.
์ด๋ ๊ฒ ํ๋ฉด ํ ์์ data๋ ํญ์ PlansType[]์ด๊ณ , undefined๊ฐ ๋์ง ์๋๋ค.
useQuery<PlansType[]>({
queryKey: ['calendarPlans'],
queryFn: fetchPlans,
initialData: [], // โ
plans๋ ํญ์ [] ์ด์์ ๋ฐฐ์ด์ด ๋จ
});
๋ฌธ์ ์ํฉ : ์บ๋ฆฐ๋์ ์ผ์ ์ด ๋ํ๋์ง ์์
ํ์ง๋ง ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์บ๋ฆฐ๋์ ์ผ์ ์ด ๋ํ๋์ง ์๋ ๋ค๋ ๊ฒ์ด๋ค.
์ด๋์๋ถํฐ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ์ธ์ง ์ฝ์์ ๋ชจ๋ ์ฐ์ด๋ณด์๋ค.
supabase์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ก์ง, useQuery์์ ์ ์ฉํ๋ ๋ถ๋ถ, ์บ๋ฆฐ๋ ์ปดํฌ๋ํธ
์ฝ์ ์ฐ์ ๋ถ๋ถ๋ค
// plans ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ - calendar ์ฌ์ฉ
export const getPlans = async (): Promise<PlansType[]> => {
const { data: plans, error } = await supabase
.from('plans')
.select('plan_id, user_id, contacts_id, title, detail, priority, start_date, end_date');
console.log('getPlans: plans', plans); โญ๏ธ
if (error) {
throw new Error(error.message);
}
return plans;
};
export const useGetCalendarPlans = () => {
return useQuery<CalendarEventType[]>({
queryKey: [QUERY_KEY.CALENDAR_PLANS],
queryFn: async () => {
const plans: PlansType[] = await getPlans(); // supabase์์ plans ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
console.log('useQuery: plans', plans); โญ๏ธ
// ์บ๋ฆฐ๋์ ์ ์ฉ๋๋๋ก ๋ฐ์ดํฐ ๊ฐ๊ณตํ๊ธฐ
const events: CalendarEventType[] = plans.map((plan) => ({
id: plan.plan_id,
title: plan.title,
start: new Date(plan.start_date),
end: new Date(plan.end_date),
}));
console.log('useQuery: events', events); โญ๏ธ
return events;
},
initialData: [], // undefined ์๋ ๋ช
์
staleTime: 60 * 1000, // 1๋ถ
});
};
const MainCalendar = () => {
const { data: events, isPending, isError, error } = useGetCalendarPlans();
console.log('MainCalendar: events', events); โญ๏ธ

ํ์ง๋ง ๋น ๋ฐฐ์ด๋ก ์ฐํ๋ฉด์ ์บ๋ฆฐ๋์๋ ์ผ์ ์ด ๋ํ๋์ง ์์๋ค.
์ฌ๋ฌ ์์ธ์ ์ฐพ์ผ๋ฉด์ ์๊ฐ์ด ์ข ์ง๋์, ๊ฐ์๊ธฐ ์ผ์ ๊ณผ ์ฝ์์ด ๋ค์ ๋ํ๋ฌ๋ค.

๋ญ๊น.. ๋ญ๊น...
๋ญ๊ฐ ๋ด๊ฐ ์ค์ ํ 1๋ถ์ staleTime๊ณผ ๊ด๋ จ์ด ์์ ๊ฒ ๊ฐ์๋ค.
์ค๋ฅ ์์ธ ๋ฐ initialData ์ฉ๋
์ฒ์ ์๋ก๊ณ ์นจ ์ ์ฝ์์ด ์ฐํ๋ ๋ชจ์ต์ ์๋์ ๊ฐ์ด ๋น ๊ฐ(undefined)์ด ๋ค์ด์๋ค๊ฐ ๊ฐ์ด ๋ค์ด์จ๋ค.
์์ธ์ ๋ฐ๋ก ์ด๊ฒ์ด์๋ค.

initialData ๋ฅผ ๋น ๋ฐฐ์ด๋ก ์ค์ ํด๋์์ผ๋, ์ฒ์ ๋น ๋ฐฐ์ด์ธ ๊ฐ์ ์บ์ฑํด๋๋ ๊ฒ์ด๋ค.
๊ทธ ํ staleTime(1๋ถ)์ด ์ง๋๋ฉด ์๋กญ๊ฒ ๊ฐ์ ํ์นญํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ค๋ ๊ฒ์ด๋ค.
์ฆ, initialData์ ์ฉ๋๋ ์๋ก์ด ๊ฐ์ ๋ฐ์์ค๊ธฐ ์ ์ ๋น ๋ฅด๊ฒ ๊ฐ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํ ๊ฒ์ด๋ค.
์๋ฒ ์ฌ์ด๋ ๋ ๋๋ง ๋ฐ์ดํฐ ์๊ฑฐ๋, ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ๋ฐ์ดํฐ ๋ด๋ ค์ฃผ๋ ๋ฑ ์ง์ง ์ด๊ธฐ๊ฐ์ ์ํด ์ฐ์ด๊ฒ ๋๋ ๊ฒ์ด๋ค.
'Forntend > Trouble Shooting' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [LoL info App] 0314 ํธ๋ฌ๋ธ์ํ (0) | 2025.03.17 |
|---|---|
| [LoL info App] 0313 ํธ๋ฌ๋ธ์ํ (0) | 2025.03.17 |
| [/\/] fetch ์ต์ ์ ํ์์ฑ (0) | 2025.03.13 |
| vercel ๋ฐฐํฌ ์ค๋ฅ - ์๋ก๊ณ ์นจ 404: NON_FOUND (0) | 2025.02.09 |
| [Pokemon PJ_Day 3]Dynamic Route์ QueryString (0) | 2025.02.06 |