T
TanStack3y ago
other-emerald

react-queryv5 showing toast onSuccess

I have a custom hook
/* eslint-disable @typescript-eslint/no-unused-vars */
import { queryKeys } from "@/app/constants/queryKeys";
import { ExchangeRates } from "@/app/types/Currencies";
import { CURRENCIES_API } from "@/config/api";
import { useCurrencyStore } from "@/src/currencyStore";
import { useQuery } from "@tanstack/react-query";
import { useEffect } from "react";
import { toast } from "react-hot-toast";

export const useFetchCurrenciesQuery = () => {
const { setCurrencyRate } = useCurrencyStore();
const getCurrencyDataFn = async () => {
const response = await fetch(CURRENCIES_API);
const result: ExchangeRates = await response.json();

return result.data;
};

const {
data: currencyRate,
error,
isLoading,
} = useQuery({
queryKey: [queryKeys.currencyRate],
queryFn: getCurrencyDataFn,

// select: (data) => {
// if (!data) return [];
// return Object.entries(data).map(([label, value]) => ({
// label,
// value,
// }));
// },
staleTime: 2 * 60 * 1000,
});

useEffect(() => {
if (error) {
toast.error("Error fetching currency options");
}
}, [error]);

useEffect(() => {
if (currencyRate) {
setCurrencyRate(currencyRate);

toast.success("Fetched data successfully", { id: "1" });
}
}, [currencyRate, setCurrencyRate]);

return {
currencyRate,
isLoading,
};
};
/* eslint-disable @typescript-eslint/no-unused-vars */
import { queryKeys } from "@/app/constants/queryKeys";
import { ExchangeRates } from "@/app/types/Currencies";
import { CURRENCIES_API } from "@/config/api";
import { useCurrencyStore } from "@/src/currencyStore";
import { useQuery } from "@tanstack/react-query";
import { useEffect } from "react";
import { toast } from "react-hot-toast";

export const useFetchCurrenciesQuery = () => {
const { setCurrencyRate } = useCurrencyStore();
const getCurrencyDataFn = async () => {
const response = await fetch(CURRENCIES_API);
const result: ExchangeRates = await response.json();

return result.data;
};

const {
data: currencyRate,
error,
isLoading,
} = useQuery({
queryKey: [queryKeys.currencyRate],
queryFn: getCurrencyDataFn,

// select: (data) => {
// if (!data) return [];
// return Object.entries(data).map(([label, value]) => ({
// label,
// value,
// }));
// },
staleTime: 2 * 60 * 1000,
});

useEffect(() => {
if (error) {
toast.error("Error fetching currency options");
}
}, [error]);

useEffect(() => {
if (currencyRate) {
setCurrencyRate(currencyRate);

toast.success("Fetched data successfully", { id: "1" });
}
}, [currencyRate, setCurrencyRate]);

return {
currencyRate,
isLoading,
};
};
and what i tried is to show toast.success when data is fetched with now errors, because onSuccess flag seems to be deprecated in v5 i used useEffect, the issue is toast will be shown everytime when i change routes so if i fetch data on home toast will be shown then i go to about page going back to home page and toast displays again, how to resolve it?
1 Reply
foreign-sapphire
foreign-sapphire3y ago
This blog post could be very useful to you https://tkdodo.eu/blog/breaking-react-querys-api-on-purpose

Did you find this page helpful?