Conditional Fetching
Sometimes you don’t want to fetch at all. The user isn’t logged in yet, a dependent value hasn’t loaded, or a feature flag is off. In these cases, the fetch should simply not happen.
Pass null as the key and SWR skips the fetch entirely, returning an idle result with no loading state.
Null Key Pattern
Section titled “Null Key Pattern”var result = swr.Use<User>( isAuthenticated ? "/api/user" : null, url => http.GetFromJsonAsync<User>(url));When the key is null:
Dataisdefault(null for reference types)IsLoadingisfalseCompletedis already resolved- The fetcher is never called
When the condition becomes true and you call Use again with a real key, SWR fetches normally.
With Composite Keys
Section titled “With Composite Keys”The same pattern works with tuple keys — pass null as the URL part:
var result = swr.Use<User, string>( token is not null ? ("/api/user", token) : (null, default!), (url, tok) => FetchWithToken(url, tok));var result = swr.Use<Product[], string, string>( tenantId is not null ? ("/api/products", tenantId, locale) : (null, default!, default!), (url, tenant, loc) => FetchProducts(url, tenant, loc));Dependent Fetching
Section titled “Dependent Fetching”Chain multiple SWR calls where one depends on another’s result:
var userResult = swr.Use<User>( "/api/user", url => http.GetFromJsonAsync<User>(url));
var ordersResult = swr.Use<Order[]>( userResult.Data is not null ? $"/api/orders/{userResult.Data.Id}" : null, url => http.GetFromJsonAsync<Order[]>(url));The orders fetch only runs once user data is available. Until then, ordersResult sits idle with no loading spinner.
When to Use Conditional Fetching
Section titled “When to Use Conditional Fetching”- Authentication guards: Don’t fetch user-specific data until the user is logged in
- Dependent data: Wait for a parent resource before fetching child resources
- Feature flags: Skip fetches for disabled features
- Form state: Only fetch validation data after the user provides required input