# Feature flags
This area is made up of several areas:
- A `FeatureFlags` context provider that determines what flags are enabled within a React tree
- A `useFeatureFlag` hook that allows a component to check if a flag is enabled
- A `FeatureFlagScope` class that acts as the value being passed around in
context. It allows us to combine and merge multiple groups of feature flags
together in a React tree
- A `GlobalFeatureFlags` value that is used as the default context value. It
holds all flags that are enabled by default
## FeatureFlags
This component acts as the context provider for feature flags in a React
application or sub-tree. It accepts a `flags` prop that specifies which the
state of feature flags.
```tsx
const defaultFeatureFlags = {
enable_new_feature: true,
}
function App() {
return (
// Note: the value of `flags` should be memoized or initialized outside of
// render
<FeatureFlags flags={defaultFeatureFlags}>
<Content />
</FeatureFlags>
)
}
```
This component is primarily used at the root of an application. However, it may
also be used for specific sub-trees, as well, to provide different feature flags
for specific routes or areas of an application.
## useFeatureFlag
The `useFeatureFlag` hook allows a component to determine if a given feature
flag is enabled. The component may use this hook to conditionally alter the
behavior of a component or render a different component altogether.
### Change the behavior of a handler based on a feature flag
```tsx
function ExampleComponent(props) {
const enabled = useFeatureFlag('enable_new_feature')
function onClick() {
if (enabled) {
// ...
} else {
// ...
}
}
// ...
}
```
### Change the behavior of a component based on a feature flag
```tsx
function ExampleComponent(props) {
const enabled = useFeatureFlag('enable_new_feature')
if (enabled) {
return <ExampleComponentNext {...props} />
}
return <ExampleComponentClassic {...props} />
}
```
> [!NOTE]
> In scenarios where you are branching between two different components, it may
> be helpful to use [function overloads](https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads) in order for the types to be inferred correctly
```tsx
function ExampleComponent(props: ClassicProps): React.ReactNode
function ExampleComponent(props: NextProps): React.ReactNode
function ExampleComponent(props: ClassicProps | NextProps): React.ReactNode {
//
}
```
By default, using `ClassicProps | NextProps` as the type signature would allow
both props to be applied to a component. Using the function overload, TypeScript
will error if you mix between the two.
## Testing
Use the `FeatureFlags` component to set the value of specific feature flags
during tests.
```tsx
render(
<FeatureFlags flags={{enableNewFeature: true}}>
<ExampleComponent />
</FeatureFlags>,
)
```
余十步
- 粉丝: 1679
- 资源: 172
最新资源
- opc da 转opc ua 、opc 隧道软件 注意:这是两个软件,安装在同一个机器上,可以实现opc da转 opc ua 安装在两个计算机上就可以实现opc tunnel功能,不需要配置d
- java Springboot网上音乐商城(源码+sql+论文)-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 档案管理系统_g2p7x--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- ffmpeg and EasyDarwin
- elk-demo 代码例子,123 45678
- 大学生创新创业训练项目管理系统设计与实现-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 大健康养老公寓管理系统_to14d-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 大学新生报到系统的设计与实现-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 基于Hadoop的高校固定资产管理系统研究与实现_hot14-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 基于Java的大学生迎新系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- JavaSpringboot+vue图书购物商城管理系统(源码+sql+论文)-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- JavaSpringboot学生教务管理系统(源码+sql+文档)-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 个性化电影推荐系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 儿童性教育网站-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- JavaSpringboot+vur前后端分离党员信息管理系统(源码+sql+论文)-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.rar
- 430大神asp.net基于三层商品进销存管理系统毕业课程源码设计
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈