OnSuccess 대신 방법

import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';

import { useQuery } from '@tanstack/react-query';
import KStadiumAPI from 'apis/kstadiumAPI';
import { selectUserSlice } from 'store';

const useCheckReferralCode = (code: string, isValid = false) => {
  const [errorMsg, setErrorMsg] = useState<string>(``);

  const { userData } = useSelector(selectUserSlice);
  const { data, isSuccess, refetch } = useQuery(
    [`CHECK_REFERRAL_CODE_KEY`, code, isValid],
    () => KStadiumAPI.getReferralCode(code),
    {
      refetchOnWindowFocus: false,
      enabled: !!code && isValid,
      // eslint-disable-next-line consistent-return
      // onSuccess: (data): string | undefined => {
      //   if (data.data?.userId === null) {
      //     console.log(`Invalid referral code. Please try again.`);
      //     return `Invalid referral code. Please try again.`;
      //   }
      //   if (data.data?.userId === userData!.userId) {
      //     console.log(`You cannot register using your own referral code.`);
      //     return `You cannot register using your own referral code.`;
      //   }
      // },
    },
  );

  useEffect(() => {
    if (data === undefined) return 
		setErrorMsg(``);

    if (data?.data?.userId === null) {
      setErrorMsg(`Invalid referral code. Please try again.`);
    }

    if (data?.data?.userId === userData!.userId) {
      setErrorMsg(`You cannot register using your own referral code.`);
    }
  }, [data, userData]);

  return { data, isSuccess, refetch, errorMsg };
};
export default useCheckReferralCode;

⭐️코드를 작성하며 질문했던 내용들

☘️찾아보기