T
TanStack3y ago
quickest-silver

Why onSuccess isn't called when query is revalidated?

I've noticed that useQuery prop onSuccess isn't called when stale query is refetched and result data is the same. Is there a way to overcome this?
8 Replies
flat-fuchsia
flat-fuchsia3y ago
you can turn off structural sharing, which makes sure that we don't inform observers if data is considered "the same", but it will mean every refetch will trigger a referentially new data object, thus also a re-render
quickest-silver
quickest-silverOP3y ago
Yeah, that wouldn't be great. Will try to look for another solution then.
flat-fuchsia
flat-fuchsia3y ago
what are you doing in onSuccess that needs to be done even if data doesn't change ?
quickest-silver
quickest-silverOP3y ago
Query is used to fetch a chat list, and I want the scrollable container to scroll down whenever new messages are fetched and when this component is remounted. Because on remount the query is revalidated, it was convenient to do it in the onSuccess.
flat-fuchsia
flat-fuchsia3y ago
okay but why would you need to scroll if data is "the same" ?
quickest-silver
quickest-silverOP3y ago
That's so the scroll should happen when a component is remounted (going from one chat to another). A refetch happens, but onSuccess isn't called.
flat-fuchsia
flat-fuchsia3y ago
then a useEffect is likely the better place
quickest-silver
quickest-silverOP3y ago
Yup, that's how I currently solved it. Thanks for checking my issue 👍

Did you find this page helpful?