@featbit/react-native-sdk

中文文档
4.4.2发布于 2026/11/04MITTypeScriptCommonJS近 30 天下载 8951

FeatBit client SDK for React Native

原始使用文档
文档目录9 节
  1. Introduction
  2. Getting started
  3. Install
  4. Prerequisite
  5. Quick Start
  6. Examples
  7. SDK
  8. Initializing the SDK
  9. Lifecycle-aware streaming

FeatBit Client-Side SDK for React Native

Introduction

This is the React Native SDK for the feature management platform FeatBit.

Be aware, this is a client side SDK, it is intended for use in a single-user context, which can be mobile, desktop or embeded applications. This SDK is designed to be used for React Native projects.

The React Native SDK is based on the React Client SDK
The React Native SDK builds on FeatBit's React Client SDK (the latter depends on the JavaScript client SDK) to provide a better integration for use in React Native applications. As a result, much of the React Client SDK and JavaScript SDK functionality is also available for the React Native SDK to use.

Getting started

Install

npm install @featbit/react-native-sdk

Prerequisite

Before using the SDK, you need to obtain the environment secret and SDK URLs.

Follow the documentation below to retrieve these values

Quick Start

The following code demonstrates:

  1. Initialize the SDK
  2. Evaluate flag with userFlags hook
// src/TestComponent.tsx
import React from 'react';
import {Text, View} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import {useFlags} from '@featbit/react-client-sdk';

export default function TestCompoment({isDarkMode}: {isDarkMode: boolean}) {
    const {robot} = useFlags();
    return (
        <View
            style={[
                {
                    marginTop: 32,
                    paddingHorizontal: 24,
                },
            ]}>
            {robot === 'AlphaGo' && (
                <Text
                    style={[
                        {
                            color: isDarkMode ? Colors.white : Colors.black,
                            textAlign: 'center',
                            fontSize: 30,
                            fontWeight: '700',
                        },
                    ]}>
                    AlphaGo 🤖
                </Text>
            )}
        </View>
    );
}

// App.tsx
import React from 'react';
import {
    SafeAreaView,
    useColorScheme
} from 'react-native';
import type { PropsWithChildren } from 'react';
import {buildConfig, withFbProvider} from '@featbit/react-native-sdk';
import TestCompoment from './src/TestComponent.tsx';

function App(): React.JSX.Element {
    const isDarkMode = useColorScheme() === 'dark';

    const backgroundStyle = {
        backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
    };

    return (
        <SafeAreaView style={backgroundStyle}>
            <TestCompoment isDarkMode={isDarkMode} />
        </SafeAreaView>
    )
}

const options = {
    user: {
        name: 'the user name',
        keyId: 'fb-demo-user-key',
        customizedProperties: []
    },
    sdkKey: 'YOUR ENVIRONMENT SECRET',
    streamingUri: 'THE STREAMING URL',
    eventsUrl: 'THE EVENTS URL'
};

export default withFbProvider(buildConfig({options}))(App);

Examples

SDK

The React Native SDK depends on the React Client SDK, please refer to the React Client SDK for more information. The Main difference between the React Client SDK and the React Native SDK is the way the SDK is initialized.

Initializing the SDK

After you install the dependency, initialize the React SDK. You can do this in one of two ways:

  • Using the asyncWithFbProvider function
  • Using the withFbProvider function

Before using either of the above ways, you need to create a IOption object and please make sure that you have called the buildConfig function with the IOption object as an argument.

function App(): React.JSX.Element {
    // The App component code ...
}

const options = {
    user: {
        name: 'the user name',
        keyId: 'fb-demo-user-key',
        customizedProperties: []
    },
    sdkKey: 'YOUR ENVIRONMENT SECRET',
    streamingUri: 'THE STREAMING URL',
    eventsUrl: 'THE EVENTS URL'
};

// This line is needed to initialize the SDK
const config = buildConfig({options});

export default withFbProvider(config)(App);

Lifecycle-aware streaming

When using streaming (dataSyncMode: 'streaming'), the SDK is lifecycle-aware out of the box. A WebSocket does not survive iOS background suspension or Android doze, and the OS may never deliver a clean close, so a naive client keeps pinging a dead socket and reconnects blindly on resume. To avoid this, the SDK drops the connection shortly after the app is backgrounded or goes offline and re-establishes it — with an immediate resync — on foreground / when connectivity returns.

This is driven by React Native's AppState and is enabled automatically; there is nothing to wire up. Brief interruptions (the iOS app switcher, momentary network blips) are debounced so they don't churn the connection. If the app never backgrounds and never reports a network change, behaviour is identical to an always-on connection.

Network-loss detection is optional. If you also install @react-native-community/netinfo, the stream additionally pauses on network loss and resumes on reconnect; without it, AppState alone drives the behaviour.

npm install @react-native-community/netinfo

版本与发布记录

@featbit/react-native-sdk 共发布 27 个版本,首次发布于 2024/05/20;过去 12 个月发布了 17 个版本。

版本发布日期距今
4.4.2latest2026/11/0410 天前
4.4.12026/10/3114 天前
4.4.02026/10/3014 天前
4.3.62026/10/1926 天前
4.3.52026/08/172 个月前
4.3.32026/08/133 个月前

关于 @featbit/react-native-sdk 的常见问题

@featbit/react-native-sdk 是什么?

FeatBit client SDK for React Native

如何安装 @featbit/react-native-sdk?

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

@featbit/react-native-sdk 支持 TypeScript 吗?

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

@featbit/react-native-sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@featbit/react-native-sdk 可以商用吗?

@featbit/react-native-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@featbit/react-native-sdk 的下载量有多少?

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

@featbit/react-native-sdk 还在维护吗?

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