Unlocking New Features Without the Hassle: Firebase Remote Config for React Devs
Pramahadi Tama Putra
Posted on July 28, 2024
Hey React devs! Ever wanted to roll out new features to your app without the usual hassle of updating and redeploying? Don’t worry, I’ve got you covered. In this guide, we’re diving into how to easily manage feature flags using Firebase Remote Config. Let’s get started!
What’s a Feature Flag?
Think of a feature flag as a handy switch in your app. It lets you turn features on or off without needing to update the app. For example, if you have a new feature you want to test on a small group of users first, feature flags make this super easy!
Why Firebase Remote Config?
Firebase Remote Config works like a remote control for your app. It allows you to change app settings or enable features from anywhere, without having to push updates to your app. With Remote Config, you can activate new features for some users before rolling them out to everyone. How cool is that?
Here’s how to set up feature flags in your React app using Firebase Remote Config.
1. Set Up Your React Project with Vite
To get started quickly with a modern and fast development environment, we’ll use Vite. Create a new React TypeScript project with:
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
2. Install Firebase
Add Firebase to your project with:
npm install firebase
3. Configure Firebase
Create a file named firebase-config.ts in the src folder and add your Firebase configuration:
import { initializeApp } from "firebase/app";
import { getRemoteConfig } from "firebase/remote-config";
// Your Firebase configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID",
measurementId: "YOUR_MEASUREMENT_ID",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Get Remote Config
const remoteConfig = getRemoteConfig(app);
remoteConfig.settings = {
minimumFetchIntervalMillis: 5000, // 5 seconds for testing only
fetchTimeoutMillis: 5000, // 5 seconds for testing only
};
export { remoteConfig };
Replace firebaseConfig with the credentials you get from the Firebase Console.
4. Create a Custom Hook
To make things easier, create a custom hook in hooks/useRemoteConfig.ts :
import { useEffect, useState } from "react";
import { remoteConfig } from "../firebase-config";
import { fetchAndActivate, getValue } from "firebase/remote-config";
const useRemoteConfig = (key: string) => {
const [value, setValue] = useState<boolean | undefined>();
useEffect(() => {
const fetchConfig = async () => {
try {
await fetchAndActivate(remoteConfig);
const fetchedValue = getValue(remoteConfig, key).asBoolean();
setValue(fetchedValue);
} catch (error) {
console.error("Failed to fetch Remote Config", error);
}
};
fetchConfig();
}, [key]);
return value;
};
export default useRemoteConfig;
5. Use the Hook in Your Component
Now, integrate this hook into your component, for example, in App.tsx :
import React from "react";
import useRemoteConfig from "./hooks/useRemoteConfig";
const App: React.FC = () => {
// Get the feature flag value for 'new_feature'
const newFeatureEnabled = useRemoteConfig("new_feature");
console.log(newFeatureEnabled);
return (
<div>
<h1>Welcome to Our App!</h1>
{newFeatureEnabled ? (
<p>New Feature is Live! 🚀</p>
) : (
<p>New Feature is Not Available Yet. 🙁</p>
)}
</div>
);
};
export default App;
Here, we’re checking the value of the 'new_feature' key. If it’s 'true', the new feature will be visible in your app.
6. Set Up Feature Flags in Firebase Console
Finally, configure your feature flags in the Firebase Console:
Open Firebase Console: Go to Firebase Console.
Select Your Project: Choose your project from the list.
Go to Remote Config: Click on Remote Config in the left menu.
Add Parameter: Click “Add Parameter” and enter the key 'new_feature' with the value 'true' or 'false', depending on what you need.
Publish: Make sure to click “Publish Changes” to save your settings.
Conclusion
That’s it! With Firebase Remote Config, you can manage and roll out new features without the usual deployment headaches. This tool makes your app development process more flexible and efficient.
If you’ve got any questions or need more details, feel free to drop a comment below. Happy coding and enjoy unlocking new features with ease! 🚀💻
Posted on July 28, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
July 28, 2024