T
TanStack3y ago
stormy-gold

Trigger select in useQuery after arbitrary event

Hello! My select business logic depends on an external state. When the external state is updated I want that select to re-evaluate the result and maybe re-render component if structuralSharing advises for it. Is there any way to trigger select in useQuery utilizing internal react-query API avoiding network request/invalidation? PS: Majority of my useQuery calls have this kind of select with external state dependency, so global refresh() method would be handy, something like QueryCache.refresh()
3 Replies
typical-coral
typical-coral3y ago
i don't think you're supposed to include external state in your queries select, without having it being part of the query key. you should compute the derived state separately and the component would rerender when your externalState changes
const { data } = useMyQuery(dependencies, { select: x => x })
const derivedData = deriveData(data, externalState)
const { data } = useMyQuery(dependencies, { select: x => x })
const derivedData = deriveData(data, externalState)
stormy-gold
stormy-goldOP3y ago
Thanks for your response. I have this solution implemented. Just looking other ways where I can benefit from select and structuralSharing props
typical-coral
typical-coral3y ago
if your external state is hooked to react rendering, then components having access to it would get re-rendered anyhow.. so you might end up doing a lot more dancing around it than necessary

Did you find this page helpful?