
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
@ttoss/react-feature-flags
Advanced tools
React Feature Flags is a library that allows you to easily add feature flags to your React application using ttoss ecosystem.
pnpm add @ttoss/react-feature-flags
Initialize the library by wrapping your application with FeatureFlagsProvider
and passing loadFeatures
function as a prop (loadFeatures
is not required). loadFeatures
function should return a promise that resolves to an object with feature flags.
import {
FeatureFlagsProvider,
useFeatureFlag,
} from '@ttoss/react-feature-flags';
/**
* Load features from your backend or any other source.
*/
const loadFeatures = async () => {
const response = await fetch('https://...');
const { features } = await response.json();
return features; // features is string[]
};
const App = () => {
return (
<FeatureFlagsProvider loadFeatures={loadFeatures}>
<MyComponent />
</FeatureFlagsProvider>
);
};
Use useFeatureFlag
hook to get a feature flag value.
import { useFeatureFlag } from '@ttoss/react-feature-flags';
const MyComponent = () => {
const isFeatureEnabled = useFeatureFlag('my-feature');
return <div>{isFeatureEnabled ? 'Enabled' : 'Disabled'}</div>;
};
useFeatureFlag
hookYou can use useFeatureFlag
hook to get a feature flag value. It returns true
if the feature flag is enabled, false
otherwise.
import { useFeatureFlag } from '@ttoss/react-feature-flags';
const MyComponent = () => {
const isFeatureEnabled = useFeatureFlag('my-feature');
return <div>{isFeatureEnabled ? 'Enabled' : 'Disabled'}</div>;
};
FeatureFlag
componentYou can use FeatureFlag
component to render its children only if the feature flag is enabled. It has a fallback
(optional) prop that can be used to render something else if the feature flag is disabled.
import { FeatureFlag } from '@ttoss/react-feature-flags';
const MyComponent = () => {
return (
<FeatureFlag name="my-feature" fallback={<div>Feature is disabled</div>}>
<div>Feature is enabled</div>
</FeatureFlag>
);
};
You can update feature flags by calling updateFeatures
function that is returned from useFeatureFlags
hook. This is useful when you want to update feature flags after providers are initialized.
import { useFeatureFlags } from '@ttoss/react-feature-flags';
const MyComponent = () => {
const { updateFeatures } = useFeatureFlags();
const handleClick = async () => {
const response = await fetch('https://...');
const { features } = await response.json();
updateFeatures(features);
};
return <button onClick={handleClick}>Update features</button>;
};
If you are using TypeScript, you can define your feature flags names on feature-flags.d.ts
file.
import '@ttoss/react-feature-flags';
declare module '@ttoss/react-feature-flags' {
export type FeatureFlags = 'my-feature' | 'my-other-feature';
}
This will allow you to use useFeatureFlag
hook with type safety.
import { useFeatureFlag } from '@ttoss/react-feature-flags';
const MyComponent = () => {
const isFeatureEnabled = useFeatureFlag('my-feature');
return <div>{isFeatureEnabled ? 'Enabled' : 'Disabled'}</div>;
};
FAQs
React Feature Flags
The npm package @ttoss/react-feature-flags receives a total of 116 weekly downloads. As such, @ttoss/react-feature-flags popularity was classified as not popular.
We found that @ttoss/react-feature-flags demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.