Blog>
Snippets

Dynamic Configuration Using Runtime Variables

Illustrate the use of TanStack Config to dynamically adjust application settings based on runtime conditions, such as user preferences or geographic location.
import { defineConfig } from 'tanstack-config';

export default defineConfig({
  // Base settings
  theme: 'light',
  apiBaseUrl: 'https://api.example.com',

  // Dynamic configuration based on user's preferences
  dynamic: ({ userPreferences }) => ({
    theme: userPreferences.theme || 'light',
    apiBaseUrl: userPreferences.region === 'EU' ? 'https://eu.api.example.com' : 'https://api.example.com',
  })
});
This code demonstrates how to use TanStack Config to define a base configuration and then override it dynamically based on runtime variables such as user preferences. The dynamic section utilizes a function that adjusts the theme and API base URL based on the user's preferences and geographic region.