// 1. 타입 지정하기
// types/profile.ts
// 프로필 조회
export interface IUserProfile {
nickname: string;
email: string;
point: number;
profileImage: string | null;
score: number;
}
// api 통신 (get)
// service/profile/api.ts
import { axiosAuthInstance } from "@/apis/axiosInstance"; // 로그인한 토큰 정보
import { APIResponse } from "@/types/model"; // api res 형식 지정
import {IUserProfile } from "@/types/profile";
// 프로필 조회 통신
export const getUserProfileReq =async (nickname: string): Promise<APIResponse<IUserProfile>> => {
const { data } = await axiosAuthInstance.get(`members/profiles/${nickname}`);
console.log(data)
return data
}
// hooks/profile/useProfile.ts
// hook에서 react-query
import { getUserProfileReq } from "@/service/profile/api"
import { useQuery } from "@tanstack/react-query"
export const useProfile = () => {
const useUserProfile = (nickname: string) => {
return useQuery({
queryKey: ['profile', nickname],
queryFn: () => getUserProfileReq(nickname),
})
}
return { useUserProfile }
}
// 사용할 때는
import { useProfile } from "@/hooks/profile/useProfile"
const { useUserProfile } = useProfile()
const {
data: userProfileInfo,
} = useUserProfile('변수로 넣기') // 임의 닉네임
>> `${userProfileInfo?.data.nickname}` 이런식으로 사용
프로필 조회 memsbers/profiles/${nickname}
경로로 보내는 경우
import { create } from 'zustand';
interface ITab {
tab: 'buy' | 'sale';
setTab: (tab: 'buy' | 'sale') => void;
}
const useTabStore = create<ITab>(set => ({
tab: 'sale',
setTab: newTab => set({ tab: newTab }),
}));
export default useTabStore;