Skip to content

Export QueryClientContext (or getQueryClientContext) for access by class components #2490

Answered by TkDodo
steevsachs asked this question in Ideas
Discussion options

You must be logged in to vote

it's an implementation detail the library uses react-context under the hood. If you want to use it in class components, a render props wrapper is the best way to do it:

const WithQuery = ({ queryKey, queryFn, options, children }) => children(useQuery(queryKey, queryFn, options))
const WithQueryClient = (children) => children({ queryClient: useQueryClient() })

usage in render of a class component:

<WithQuery queryKey="todos" queryFn={fetchTodos}>
  ({ data, isLoading, isError } => ...
</WithQuery>

<WithQueryClient>
  {(queryClient) => ... }
</WithQueryClient>

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@steevsachs
Comment options

Answer selected by steevsachs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants