DEV Community

Cover image for Taking Feature Flags to the Next Level: Simplified Feature Flags and Dynamic Overrides
Andrew McCallum
Andrew McCallum

Posted on

Taking Feature Flags to the Next Level: Simplified Feature Flags and Dynamic Overrides

As developers, we know the importance of feature flags in modern web applications. They allow us to control the release and activation of features with ease, enabling smooth experimentation and testing. Today, we are thrilled to introduce use-feature, a powerful React NPM package designed to streamline the process of managing feature flags within your React applications.

What is use-feature?

use-feature is a versatile React hook that simplifies the implementation of feature flags in your React apps. With support for all the main React frameworks, including Gatsby, Next, Vite, and Create-react-app, integrating feature flags has never been more accessible.

Why Use use-feature?
  1. Flexibility and Customisation: use-feature is designed with flexibility in mind. It allows you to set feature flags using a boolean variable or look for environment variables in your .env files. This means you can configure feature flags based on different conditions, tailoring the behaviour to suit your specific use cases.

  2. Dynamic Overrides: One of the standout features of use-feature is its ability to be dynamically overridden. This means you can enable or disable a feature on the fly using query strings, cookies, or local storage. It's particularly useful when you want to have a feature disabled in production but enable it temporarily for specific users or for yourself during development and testing.

  3. Seamless Integration: Integrating use-feature into your existing React projects is a breeze. With just a few lines of code, you can effortlessly add feature flags to control various aspects of your application.

Getting Started

To get started with use-feature, simply install it via NPM using the following command:

npm install use-feature
Enter fullscreen mode Exit fullscreen mode

Set your feature flag value in your .env file:

cool_feature=true
Enter fullscreen mode Exit fullscreen mode

Next, import the hook into your React component:

import { useFeature } from 'use-feature';
Enter fullscreen mode Exit fullscreen mode

Now, let's see how easy it is to create a feature flag:

function MyComponent() {
  const isCoolFeatureEnabled = useFeature('cool_feature', false);

  // Rest of your component logic
}
Enter fullscreen mode Exit fullscreen mode

In this example, the useFeature hook takes two arguments: the feature flag name ('cool_feature') and the default value (false). The hook operates in the following way: First, it checks if there is an environment variable set with the name cool_feature. If such an environment variable exists, it will use its value as the feature flag. Next, the hook checks for any possible overrides for the feature flag, which could be provided through the query string, cookie, or local storage. If an override value is available, it will be used. However, if no override is found, the hook will fall back to using the default value provided. This ensures a seamless and flexible way to handle feature flags in your React application.

Let's take a look at some real-world scenarios where use-feature can be incredibly valuable:

  1. Beta Testing: You can use use-feature to activate beta features for selected users by setting up an override through query parameters. This allows you to gather feedback and fine-tune features before a full release.

  2. A/B Testing: Run A/B tests with ease by enabling or disabling specific features for different users, directly from the query string or cookies.

  3. Internal Development: Keep a feature hidden from users but enable it for developers and testers to facilitate development and debugging.

Feature flags are essential for delivering flexible and well-tested web applications. With use-feature, you can easily manage feature flags in your React apps and even override them dynamically when needed. Its seamless integration and versatility make it a must-have tool for any React developer.

Start using use-feature today by installing the package via NPM and explore its capabilities to optimise your React app development process. For more information and detailed examples, check out the GitHub repository at https://github.com/feijoa-dev/use-feature.

We look forward to seeing the innovative ways you implement feature flags with use-feature in your React applications! Happy coding!

Top comments (0)