@featureflip/react

中文文档
2.19.1发布于 2026/11/10Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 2623

React SDK for Featureflip - React bindings for feature flag evaluation

原始使用文档
文档目录10 节
  1. Installation
  2. Quick Start
  3. Provider Props
  4. Hooks
  5. useFeatureFlag<T>(key: string, defaultValue: T): T
  6. useFeatureflipStatus(): { isReady, isError, error }
  7. useFeatureflipClient(): FeatureflipClient
  8. Identify on Login
  9. Testing
  10. License

@featureflip/react

React bindings for Featureflip feature flag evaluation.

Installation

npm install @featureflip/react @featureflip/browser

Quick Start

Wrap your app with FeatureflipProvider, then use useFeatureFlag in any component.

import { FeatureflipProvider, useFeatureFlag } from '@featureflip/react';

function App() {
  return (
    <FeatureflipProvider clientKey="your-client-sdk-key">
      <Banner />
    </FeatureflipProvider>
  );
}

function Banner() {
  const showBanner = useFeatureFlag('show-banner', false);
  if (!showBanner) return null;
  return <div>New feature available!</div>;
}

Provider Props

PropTypeDefaultDescription
clientKeystring(required)Client SDK key
contextRecord<string, unknown>undefinedEvaluation context (user attributes)
baseUrlstringundefinedEvaluation API base URL
streamingbooleanundefinedEnable SSE streaming
sendEvaluationEventsbooleantruePassed to the browser SDK; see its README.
bootstrapRecord<string, unknown> | 'localStorage'undefinedValues to render before the first server response. Passed to the browser SDK; see its README.

The provider creates a FeatureflipClient internally and calls initialize() on mount. It cleans up (calls close()) on unmount.

With a bootstrap object (for example, flag values your server evaluated while rendering the page), useFeatureFlag returns those values on the first render and useFeatureflipStatus() reports isReady: true immediately; the server's values replace them when they arrive. During server rendering the provider uses FeatureflipClient.forServerRender, which serves only the bootstrap values, makes no requests, and is not shared between requests.

Hooks

useFeatureFlag<T>(key: string, defaultValue: T): T

Returns the evaluated value of a flag. The component re-renders automatically when the flag value changes via streaming or identify(). The type of defaultValue determines which variation method is called (boolVariation, stringVariation, numberVariation, or jsonVariation).

const enabled = useFeatureFlag('new-checkout', false);
const color = useFeatureFlag('button-color', 'blue');
const limit = useFeatureFlag('rate-limit', 100);
const config = useFeatureFlag('ui-config', { sidebar: true });

useFeatureflipStatus(): { isReady, isError, error }

Returns the initialization status of the client. Useful for showing loading states.

function App() {
  const { isReady, isError, error } = useFeatureflipStatus();

  if (isError) return <div>Error: {error?.message}</div>;
  if (!isReady) return <div>Loading...</div>;

  return <Main />;
}

useFeatureflipClient(): FeatureflipClient

Returns the underlying FeatureflipClient instance for direct access (e.g., calling identify).

Identify on Login

When the context prop changes, the provider automatically calls identify() and re-evaluates all flags. You can also call identify directly.

function LoginPage() {
  const client = useFeatureflipClient();

  async function handleLogin(userId: string) {
    // ... authenticate ...
    await client.identify({ user_id: userId, plan: 'pro' });
  }

  return <button onClick={() => handleLogin('123')}>Log in</button>;
}

Testing

Use TestFeatureflipProvider to supply predetermined flag values in tests -- no network calls, no initialization delay.

import { TestFeatureflipProvider } from '@featureflip/react';
import { render, screen } from '@testing-library/react';

test('renders banner when flag is on', () => {
  render(
    <TestFeatureflipProvider flags={{ 'show-banner': true }}>
      <Banner />
    </TestFeatureflipProvider>,
  );

  expect(screen.getByText('New feature available!')).toBeInTheDocument();
});

TestFeatureflipProvider uses FeatureflipClient.forTesting() internally and sets isReady: true immediately.

License

Apache-2.0

版本与发布记录

@featureflip/react 共发布 33 个版本,首次发布于 2026/04/02;过去 12 个月发布了 33 个版本。

版本发布日期距今
2.19.1latest2026/11/104 天前
2.19.02026/11/086 天前
2.18.02026/10/3015 天前
2.17.22026/10/2817 天前
2.17.12026/10/2717 天前
2.17.02026/10/2718 天前

关于 @featureflip/react 的常见问题

@featureflip/react 是什么?

React SDK for Featureflip - React bindings for feature flag evaluation

如何安装 @featureflip/react?

在项目目录运行 npm install @featureflip/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、@featureflip/browser(^2.19.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@featureflip/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@featureflip/react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@featureflip/react 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@featureflip/react 可以商用吗?

@featureflip/react 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@featureflip/react 的下载量有多少?

根据 npm 官方下载统计,@featureflip/react 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,623 次,日均约 87 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@featureflip/react 还在维护吗?

最新版本 2.19.1 发布于 2026/11/10(4 天前);至今共发布 33 个版本,其中过去 12 个月发布了 33 个;首次发布于 2026/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。