Blog>
Snippets

Installing and Initializing TanStack Config in a React Project

Showcase how to install TanStack Config via npm, set up a basic configuration file, and initialize it within a React application.
// Step 1: Install TanStack Config via npm
npm install @tanstack/react-config
This command installs the TanStack Config package through npm. Ensure you run this in the root directory of your React project.
// Step 2: Create a config file, e.g., appConfig.js

export const appConfig = {
  apiUrl: 'https://api.example.com',
  theme: 'light'
};
This step involves creating a basic configuration file. Here, `appConfig.js` is an example where you define your application's configuration such as API URLs and theme preferences.
// Step 3: Initialize TanStack Config in your React application

// Import the ConfigProvider from TanStack Config
import { ConfigProvider } from '@tanstack/react-config';

// Import your app configuration
import { appConfig } from './appConfig';

function App() {
  return (
    <ConfigProvider config={appConfig}>
      {/* The rest of your app goes here */}
    </ConfigProvider>
  );
}

export default App;
In this final step, you initialize TanStack Config in your React application. You wrap your application's components with `ConfigProvider` from TanStack Config, passing your `appConfig` as a prop to make it available throughout your application.