Skip to content
Advertisement

Using QueryClientProvider in a Wrapper

I want to use a single Wrapper Component in a library im providing. The wrapper should provide stuff like Context and QueryClient. The component looks like this:

JavaScript

When wrapping children with the Wrapper useQuery throws an error that no QueryClientProvider is set. In this case App uses the useQuery Hook from react-query.

JavaScript

At first i thought you need a QueryClientProvider directly above an useQuery hook. But for example in Storybook you can define one QueryClient for all stories together. Where did i went wrong?

Edit: App component:

JavaScript

Advertisement

Answer

Well a few steps lead to my goal:

  1. In the library, put react-query into the devDependencies and peerDependencies.
  2. In the app, put react-query into the dependencies.
  3. In the library, exclude react-query from being bundled by adding it to your bundler config. In my case it’s the vite.config.ts:
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement