Skip to content

Commit 802d1a2

Browse files
committed
Revert "refactor: replace queryClient prop with auto-detecting parent QueryClient"
This reverts commit 3ddb6f2.
1 parent fd431a3 commit 802d1a2

File tree

3 files changed

+28
-62
lines changed

3 files changed

+28
-62
lines changed

README.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ If you do not install it, your package manager will warn you, and stac-react wil
3535

3636
stac-react's hooks must be used inside children of a React context that provides access to the stac-react's core functionality.
3737

38-
To get started, initialize `StacApiProvider` with the base URL of the STAC catalog. `StacApiProvider` automatically sets up a [TanStack Query](https://tanstack.com/query/latest/docs/framework/react/overview) QueryClientProvider for you if one doesn't already exist in the component tree.
38+
To get started, initialize `StacApiProvider` with the base URL of the STAC catalog. `StacApiProvider` automatically sets up a [TanStack Query](https://tanstack.com/query/latest/docs/framework/react/overview) QueryClientProvider for you, so you do not need to wrap your app with QueryClientProvider yourself.
3939

4040
```jsx
4141
import { StacApiProvider } from 'stac-react';
@@ -47,26 +47,19 @@ function StacApp() {
4747
}
4848
```
4949

50-
If you want to customize the QueryClient configuration (e.g., for custom caching behavior, retry logic, or global settings), wrap `StacApiProvider` with your own `QueryClientProvider`:
50+
If you want to provide your own custom QueryClient (for advanced caching or devtools), you can pass it as a prop:
5151

5252
```jsx
5353
import { StacApiProvider } from 'stac-react';
54-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
54+
import { QueryClient } from '@tanstack/react-query';
5555

56-
const queryClient = new QueryClient({
57-
defaultOptions: {
58-
queries: {
59-
staleTime: 5 * 60 * 1000, // 5 minutes
60-
retry: 3,
61-
},
62-
},
63-
});
56+
const queryClient = new QueryClient();
6457

6558
function StacApp() {
6659
return (
67-
<QueryClientProvider client={queryClient}>
68-
<StacApiProvider apiUrl="https://my-stac-api.com">{/* Other components */}</StacApiProvider>
69-
</QueryClientProvider>
60+
<StacApiProvider apiUrl="https://my-stac-api.com" queryClient={queryClient}>
61+
{/* Other components */}
62+
</StacApiProvider>
7063
);
7164
}
7265
```
@@ -117,11 +110,9 @@ function StacApp() {
117110

118111
##### Component Properties
119112

120-
| Option | Type | Description |
121-
| ---------------- | --------- | --------------------------------------------------------------------------------------------- |
122-
| `apiUrl` | `string` | The base url of the STAC catalog. |
123-
| `options` | `object` | Optional configuration object for customizing STAC API requests. |
124-
| `enableDevTools` | `boolean` | Optional. Enables TanStack Query DevTools browser extension integration. Defaults to `false`. |
113+
| Option | Type | Description |
114+
| --------- | -------- | --------------------------------- |
115+
| `apiUrl`. | `string` | The base url of the STAC catalog. |
125116

126117
### useCollections
127118

docs/react-query-setup.md

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,30 @@ stac-react relies on [TanStack Query](https://tanstack.com/query/latest/docs/fra
88
- Ensures your app and stac-react share the same QueryClient instance.
99
- Follows best practices for React libraries that integrate with popular frameworks.
1010

11-
## QueryClient Management
11+
stac-react manages the QueryClient for you by default, but you can provide your own for advanced use cases.
1212

13-
By default, `StacApiProvider` automatically creates and manages a QueryClient for you if one doesn't already exist in the component tree. This means you can use stac-react without any additional setup:
13+
**Important:** If your app uses multiple providers that require a TanStack QueryClient (such as `QueryClientProvider` and `StacApiProvider`), always use the same single QueryClient instance for all providers. This ensures that queries, mutations, and cache are shared across your app and prevents cache fragmentation or duplicate network requests.
1414

15-
```jsx
16-
import { StacApiProvider } from 'stac-react';
17-
18-
function App() {
19-
return <StacApiProvider apiUrl="https://my-stac-api.com">{/* ...your app... */}</StacApiProvider>;
20-
}
21-
```
22-
23-
### Custom QueryClient Configuration
24-
25-
If you need custom QueryClient configuration (e.g., custom caching behavior, retry logic, or global settings), wrap `StacApiProvider` with your own `QueryClientProvider`:
15+
**Example:**
2616

2717
```jsx
2818
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2919
import { StacApiProvider } from 'stac-react';
3020

31-
const queryClient = new QueryClient({
32-
defaultOptions: {
33-
queries: {
34-
staleTime: 5 * 60 * 1000, // 5 minutes
35-
retry: 3,
36-
},
37-
},
38-
});
21+
const queryClient = new QueryClient();
3922

4023
function App() {
4124
return (
4225
<QueryClientProvider client={queryClient}>
43-
<StacApiProvider apiUrl="https://my-stac-api.com">{/* ...your app... */}</StacApiProvider>
26+
<StacApiProvider apiUrl="https://my-stac-api.com" queryClient={queryClient}>
27+
{/* ...your app... */}
28+
</StacApiProvider>
4429
</QueryClientProvider>
4530
);
4631
}
4732
```
4833

49-
`StacApiProvider` will automatically detect the parent QueryClient and use it instead of creating a new one.
34+
If you do not pass the same QueryClient instance, each provider will maintain its own cache, which can lead to unexpected behavior.
5035

5136
## TanStack Query DevTools Integration
5237

src/context/index.tsx

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import React, { useMemo, useContext } from 'react';
1+
import React, { useMemo } from 'react';
22
import { StacApiContext } from './context';
33
import { GenericObject } from '../types';
4-
import { QueryClient, QueryClientProvider, QueryClientContext } from '@tanstack/react-query';
4+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
55

66
import useStacApi from '../hooks/useStacApi';
77

88
type StacApiProviderType = {
99
apiUrl: string;
1010
children: React.ReactNode;
1111
options?: GenericObject;
12+
queryClient?: QueryClient;
1213
enableDevTools?: boolean;
1314
};
1415

1516
function StacApiProviderInner({
1617
children,
1718
apiUrl,
1819
options,
19-
}: Omit<StacApiProviderType, 'enableDevTools'>) {
20+
}: Omit<StacApiProviderType, 'queryClient'>) {
2021
const { stacApi } = useStacApi(apiUrl, options);
2122

2223
const contextValue = useMemo(
@@ -33,30 +34,19 @@ export function StacApiProvider({
3334
children,
3435
apiUrl,
3536
options,
37+
queryClient,
3638
enableDevTools,
3739
}: StacApiProviderType) {
38-
const existingClient = useContext(QueryClientContext);
3940
const defaultClient = useMemo(() => new QueryClient(), []);
41+
const client: QueryClient = queryClient ?? defaultClient;
4042

41-
const client = existingClient ?? defaultClient;
42-
43-
// Setup DevTools once when component mounts or enableDevTools changes
44-
useMemo(() => {
45-
if (enableDevTools && typeof window !== 'undefined') {
46-
window.__TANSTACK_QUERY_CLIENT__ = client;
47-
}
48-
}, [client, enableDevTools]);
49-
50-
if (existingClient) {
51-
return (
52-
<StacApiProviderInner apiUrl={apiUrl} options={options}>
53-
{children}
54-
</StacApiProviderInner>
55-
);
43+
if (enableDevTools && typeof window !== 'undefined') {
44+
// Connect TanStack Query DevTools (browser extension)
45+
window.__TANSTACK_QUERY_CLIENT__ = client;
5646
}
5747

5848
return (
59-
<QueryClientProvider client={defaultClient}>
49+
<QueryClientProvider client={client}>
6050
<StacApiProviderInner apiUrl={apiUrl} options={options}>
6151
{children}
6252
</StacApiProviderInner>

0 commit comments

Comments
 (0)