new QueryClient instantiated in a function component
Hi, so I want to trigger toasts on errors, and the toasts can only be controlled through hooks. In this case, instead of instantiating the query client globally, I made a wrapper rq provider which has access to the toast provider:
So my question is: is it still okay to instantiate a client in this way? There's only 1 way to trigger this provider rerender - which is by changing the theme (as my theme provider and toast provider are coming from the same library)
4 Replies
correct-apricotOP•3y ago
I considered wrapping it in a useCallback useMemo but is it really necessary when there's really no way to rerender it too many times
like-gold•3y ago
put it in useState: https://tkdodo.eu/blog/use-state-for-one-time-initializations
useState for one-time initializations
Why you shouldn't rely on useMemo for guaranteed referential stability but prefer useState instead
like-gold•3y ago
if you change a theme, you'd delete all your queries with that. probably not what you want : https://tkdodo.eu/blog/react-query-fa-qs#2-the-queryclient-is-not-stable
React Query FAQs
Answering the most frequently asked React Query questions
correct-apricotOP•3y ago
damn, you're correct. thanks!