T
TanStack4y ago
conscious-sapphire

onSuccess (callback) only on initial load

I would like to define a callback when the data is loaded for the first time. For example, after loading the data I want to display some feedback message. I don't want to display it every time the data is fetched, only for the first time. Something like this doesn't work, as it is called after every fetch:
const {
data,
} = useQuery(['list'], fetchList, {
onError: (e) => {
dispatch(showErrorFeedbackMessage(errorMessage));
},
// Do that only after first load
onSuccess: () => {
dispatch(showErrorFeedbackMessage(errorMessage));
}
});
const {
data,
} = useQuery(['list'], fetchList, {
onError: (e) => {
dispatch(showErrorFeedbackMessage(errorMessage));
},
// Do that only after first load
onSuccess: () => {
dispatch(showErrorFeedbackMessage(errorMessage));
}
});
1 Reply
fair-rose
fair-rose4y ago
You should be able to check if there's data in the cache already

Did you find this page helpful?