Skip to content

Commit 3ddf800

Browse files
committed
feat(query-core): include current query as a placeholder data function param
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
1 parent 2e26f14 commit 3ddf800

6 files changed

Lines changed: 40 additions & 3 deletions

File tree

.changeset/long-bars-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': minor
3+
---
4+
5+
feat(query-core): include current query as a placeholder data function param

docs/framework/react/reference/useQuery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const {
155155
- `initialDataUpdatedAt: number | (() => number | undefined)`
156156
- Optional
157157
- If set, this value will be used as the time (in milliseconds) of when the `initialData` itself was last updated.
158-
- `placeholderData: TData | (previousValue: TData | undefined, previousQuery: Query | undefined) => TData`
158+
- `placeholderData: TData | (previousValue: TData | undefined, previousQuery: Query | undefined, currentQuery: Query) => TData`
159159
- Optional
160160
- If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `pending` state.
161161
- `placeholderData` is **not persisted** to the cache

docs/framework/solid/reference/useQuery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function App() {
204204
- Optional
205205
- This option can be used to transform or select a part of the data returned by the query function. It affects the returned `data` value, but does not affect what gets stored in the query cache.
206206
- The `select` function will only run if `data` changed, or if the reference to the `select` function itself changes. To optimize, wrap the function in `useCallback`.
207-
- ##### `placeholderData: TData | (previousValue: TData | undefined; previousQuery: Query | undefined,) => TData`
207+
- ##### `placeholderData: TData | (previousValue: TData | undefined, previousQuery: Query | undefined, currentQuery: Query) => TData`
208208
- Optional
209209
- If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `pending` state.
210210
- `placeholderData` is **not persisted** to the cache

packages/query-core/src/__tests__/queryObserver.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,36 @@ describe('queryObserver', () => {
10661066
expect(selectCount).toBe(3)
10671067
})
10681068

1069+
test('should pass the current query to placeholderData function', () => {
1070+
const testQueryKey = queryKey()
1071+
1072+
const placeholderDataSpy = vi.fn(
1073+
(_: unknown, __: unknown, ___: unknown) => undefined,
1074+
)
1075+
1076+
const queryClient2 = new QueryClient({
1077+
defaultOptions: {
1078+
queries: {
1079+
placeholderData: placeholderDataSpy,
1080+
},
1081+
},
1082+
})
1083+
1084+
new QueryObserver(queryClient2, {
1085+
queryKey: testQueryKey,
1086+
queryFn: () => Promise.resolve(),
1087+
})
1088+
1089+
expect(placeholderDataSpy).toHaveBeenCalledTimes(1)
1090+
expect(placeholderDataSpy).toHaveBeenCalledWith(
1091+
undefined,
1092+
undefined,
1093+
expect.objectContaining({ queryKey: testQueryKey }),
1094+
)
1095+
1096+
queryClient2.clear()
1097+
})
1098+
10691099
test('should use cached selectResult when switching between queries and placeholderData returns previousData', async () => {
10701100
const results: Array<QueryObserverResult> = []
10711101

packages/query-core/src/queryObserver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,11 @@ export class QueryObserver<
505505
placeholderData =
506506
typeof options.placeholderData === 'function'
507507
? (
508-
options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>
508+
options.placeholderData as unknown as PlaceholderDataFunction<TQueryData, TError>
509509
)(
510510
this.#lastQueryWithDefinedData?.state.data,
511511
this.#lastQueryWithDefinedData as any,
512+
query as any,
512513
)
513514
: options.placeholderData
514515
}

packages/query-core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export type PlaceholderDataFunction<
176176
> = (
177177
previousData: TQueryData | undefined,
178178
previousQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined,
179+
currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
179180
) => TQueryData | undefined
180181

181182
export type QueriesPlaceholderDataFunction<TQueryData> = (

0 commit comments

Comments
 (0)