How can I configure React Query to retain data even when errors occur? This is my code but when there is error data would change to undefined
Additionally, it appears that the onError
method is deprecated in the latest version of React-Query
Is there an alternative solution to prevent the existing data from becoming undefined in case of an error?
import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { getDashboard } from '../dashboard/query';
function Dashboard() {
const [todayDate, setTodayDate] = useState('');
const { data, isError, isSuccess, isFetching, error } = useQuery(
['dashboard', { date: todayDate }],
getDashboard,
{
keepPreviousData: true, // Keep previous data while fetching new data
retry: false,
}
onError(error) {
if (error) {
queryClient.setQueryData(['dashboard', getDashboard], query.data);
}
}
);
return (
<div>
{ isError && error ? <div>error.message < /div> : null}
<div> { data!.data.name } </div>
</div>
);
}