MCPcopy Create free account
hub / github.com/TanStack/query / setOptions

Method setOptions

packages/query-core/src/queryObserver.ts:182–264  ·  view source on GitHub ↗

* Updates the observer's options. This will re-resolve the query being * observed (switching to a different query if the `queryKey` changed), * trigger a fetch if the new options require one and the observer has * subscribers, recompute the current result, and reschedule the stale and *

(
    options: QueryObserverOptions<
      TQueryFnData,
      TError,
      TData,
      TQueryData,
      TQueryKey
    >,
  )

Source from the content-addressed store, hash-verified

180 * ```
181 */
182 setOptions(
183 options: QueryObserverOptions<
184 TQueryFnData,
185 TError,
186 TData,
187 TQueryData,
188 TQueryKey
189 >,
190 ): void {
191 const prevOptions = this.options
192 const prevQuery = this.#currentQuery
193
194 this.options = this.#client.defaultQueryOptions(options)
195
196 if (
197 this.options.enabled !== undefined &&
198 typeof this.options.enabled !== 'boolean' &&
199 typeof this.options.enabled !== 'function' &&
200 typeof resolveQueryValue(this.options.enabled, this.#currentQuery) !==
201 'boolean'
202 ) {
203 throw new Error(
204 'Expected enabled to be a boolean or a callback that returns a boolean',
205 )
206 }
207
208 this.#updateQuery()
209 this.#currentQuery.setOptions(this.options)
210
211 if (
212 prevOptions._defaulted &&
213 !shallowEqualObjects(this.options, prevOptions)
214 ) {
215 this.#client.getQueryCache().notify({
216 type: 'observerOptionsUpdated',
217 query: this.#currentQuery,
218 observer: this,
219 })
220 }
221
222 const mounted = this.hasListeners()
223
224 // Fetch if there are subscribers
225 if (
226 mounted &&
227 shouldFetchOptionally(
228 this.#currentQuery,
229 prevQuery,
230 this.options,
231 prevOptions,
232 )
233 ) {
234 this.#executeFetch()
235 }
236
237 // Update result
238 this.updateResult()
239

Callers 1

constructorMethod · 0.95

Calls 13

#updateQueryMethod · 0.95
#executeFetchMethod · 0.95
updateResultMethod · 0.95
#updateStaleTimeoutMethod · 0.95
resolveQueryValueFunction · 0.90
shallowEqualObjectsFunction · 0.90
shouldFetchOptionallyFunction · 0.85
defaultQueryOptionsMethod · 0.80
getQueryCacheMethod · 0.80
hasListenersMethod · 0.80

Tested by

no test coverage detected