Blog>
Snippets

Using TanStack Config for Feature Flagging in a React App

Demonstrate how to utilize TanStack Config for managing feature flags in a React application, allowing conditional rendering of components based on configuration.
import { useConfig } from '@tanstack/react-config';
First, import the useConfig hook from @tanstack/react-config to access the configuration in your component.
const FeatureComponent = () => {
  const config = useConfig();
  const isFeatureEnabled = config.features?.newFeature;
  
  return (
    <>
      {isFeatureEnabled && (
        <div>New Feature is Enabled!</div>
      )}
    </>
  );
};
Then, define your component that utilizes the configuration to conditionally render based on the 'newFeature' flag.
export default FeatureComponent;
Finally, export the component for use in your app. This setup allows you to toggle the visibility of the 'FeatureComponent' based on the 'newFeature' flag set in your TanStack Config.