Skip to main content

Custom Data Strategies

The dataStrategy API allows you to customize how React Router executes loaders and actions, giving you control over when and how data is fetched.

Overview

By default, React Router calls loaders in parallel and actions individually. The dataStrategy function lets you override this behavior to implement patterns like:
  • Sequential loader execution
  • Single fetch requests for all loaders (like Remix’s Single Fetch)
  • Middleware patterns with context passing
  • Custom error handling and response decoding

Basic Usage

The dataStrategy Function

The dataStrategy receives a DataStrategyFunctionArgs object:

Matches and resolve()

Each match in the matches array has a resolve() method that:
  • Waits for route.lazy to load if needed
  • Determines whether to call the loader or action
  • Handles shouldRevalidate logic internally
  • Returns a HandlerResult with the data or error

Sequential Loaders

Execute loaders one at a time, passing context between them:

Middleware Pattern

Implement middleware that runs before loaders:
Define middleware in your routes:

Single Fetch Pattern

Make one request for all loaders:

Custom Response Decoding

Decode responses using custom formats:

Error Handling

The resolve() method never throws. Errors are returned in the result:

Working with Actions and Fetchers

The dataStrategy handles actions and fetchers too. For single-match operations, you’ll receive a single-item array:

Framework Mode

In framework mode, configure dataStrategy via createRequestHandler:

Best Practices

  1. Don’t modify request/params: The handler receives its own arguments
  2. Use resolve() callbacks sparingly: Only when you need custom logic
  3. Handle shouldRevalidate: It’s already handled by resolve()
  4. Consider performance: Sequential loading can slow down your app
  5. Test both states: With and without interruptions