Skip to main content
The notifyManager is a singleton that manages notification scheduling and batching for TanStack Query. It provides utilities to batch multiple state updates together and control how notifications are executed.

Import

Methods

batch

Execute a callback and batch all notifications triggered during its execution into a single tick.
() => T
required
A function to execute. All notifications triggered during this function’s execution will be batched and flushed together.
T
Returns the result of the callback function.

Example

This is useful for performance optimization when you need to make multiple updates at once. Instead of triggering re-renders for each update, they will all be batched together.

batchCalls

Wrap a function so that all calls to it are automatically batched.
(...args: T) => void
required
The function to wrap. All calls to the returned function will be batched and executed together.
(...args: T) => void
Returns a wrapped version of the callback that batches all calls.

Example

schedule

Schedule a callback to be executed. If called within a batch, the callback will be queued and executed when the batch completes.
() => void
required
The callback function to schedule.

Example

setNotifyFunction

Set a custom function to wrap all notifications. This is useful for testing or integrating with framework-specific batching mechanisms.
(callback: () => void) => void
required
A function that will be called to execute each notification. It receives a callback that should be invoked to execute the actual notification.
This is commonly used to wrap notifications with React.act during testing or to integrate with React’s batching mechanism.

Example

setBatchNotifyFunction

Set a custom function to batch multiple notifications together. By default, TanStack Query uses the batch function provided by ReactDOM or React Native.
(callback: () => void) => void
required
A function that receives a callback containing multiple batched notifications. This function should execute the callback in a way that batches the updates together.

Example

setScheduler

Set a custom scheduler function for executing notifications.
(callback: () => void) => void
required
A function that schedules the callback to be executed. By default, this uses a zero-delay timeout (setTimeout with 0ms delay).

Example

Use Cases

Testing with React

When testing React components, you should wrap notifications with React.act to ensure proper batching:

Custom Batching Strategy

You can implement a custom batching strategy to control when updates are flushed:

Optimizing Performance

Use batch to group multiple updates and prevent unnecessary re-renders:

Synchronous Scheduling

In some cases, you may want notifications to execute synchronously:
Be cautious with synchronous scheduling as it can lead to performance issues if many notifications are triggered in quick succession.

Default Behavior

By default, the notifyManager:
  • Uses a zero-delay timeout (setTimeout(callback, 0)) for scheduling notifications
  • Executes notifications immediately (no wrapping)
  • Executes batch notifications immediately (no framework-specific batching)
These defaults work well for most use cases, but you can customize them based on your framework and testing requirements.