# @xtreamsrl/react-feature-flags

> This package exposes a collections of hooks and utilities to manage feature flags.

Latest version **0.1.4** (published 2024-09-17) · 0 weekly downloads

## Install

```sh
npm install @xtreamsrl/react-feature-flags
pnpm add @xtreamsrl/react-feature-flags
yarn add @xtreamsrl/react-feature-flags
bun add @xtreamsrl/react-feature-flags
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2024-09-17 |
| First published | 2023-05-25 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 44.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xtream |
| Maintainers | xtreamsrl |

## Links

- npm: https://www.npmjs.com/package/@xtreamsrl/react-feature-flags
- Repository: https://github.com/xtreamsrl/enterprise-react
- Homepage: https://github.com/xtreamsrl/enterprise-react#readme
- Issues: https://github.com/xtreamsrl/enterprise-react/issues
- npm.io page: https://npm.io/package/@xtreamsrl/react-feature-flags

## Recent versions

- 0.1.4 (latest) — 2024-09-17
- 0.1.3 — 2024-05-13
- 0.1.2 — 2024-05-13
- 0.1.1 — 2023-12-18
- 0.0.6 — 2023-11-23
- 0.0.5 — 2023-05-29
- 0.0.4 — 2023-05-25

## README

# @xtreamsrl/react-feature-flags

This package exposes a collections of hooks and utilities to manage feature flags.

## Installation

```shell
npm install @xtreamsrl/react-feature-flags
```

## Usage
In order to use the feature flag utilities, the package must be configured with a manger of your choice that implements `FeatureFlagManager` interface.

This can be done using one of the available managers ([growthbook](../react-feature-flags-growthbook/README.md) or [launchdarkly](../react-feature-flags-launchdarkly/README.md)) or by implementing a custom one.

Define the features you want to manage by augmenting the `Features` interface; Then assign the value of the flag (defined in the chosen manager platform) to the corresponding key within the `FeaturesMap` .

```typescript
// features.d.ts
import { Features } from '@xtreamsrl/react-feature-flags';

declare module '@xtreamsrl/react-feature-flags' {

  interface Features {
    /*
    * Here you can list your app features.
    * This interface will define the keys of the features map.
    *
    * Logout: boolean;
    *
    */
  }
}

```

```typescript
// featuresMap.ts
import { FeaturesMap } from "@xtreamsrl/react-feature-flags";

export const featuresMap: FeaturesMap = {
  /*
  * Here you can map your app features (defined in features.d.ts)
  * to the according flag keys (defined in the chosen manager platform)
  *
  * Logout: {
  *   flag: 'show-logout',
  * }
  *
  */
};

```
### Client Side Usage

Set up the feature flag manager and the retrieval hook with `configureManager` and `configureFeatureFlags`. 
Be sure to wrap the main app with the `FeatureFlagProvider`.

```tsx
// app.tsx
import { configureManager, configureFeatureFlags, FeatureFlagProvider } from '@xtreamsrl/react-feature-flags'
import { featuresMap } from '.shared/feature-flags'

configureFeatureFlags({
    useFeatureFlagValue: /* Add your hook for managing flags values here */,
});

configureManager({
manager: /* Add here an instance of a manager implementing FeatureFlagManager interface */
});

export function App() {
  return (
    <FeatureFlagProvider featuresMap={featuresMap}>
      <MainApp />
    </FeatureFlagProvider>
  );
}

```

### Feature Flag Value
To retrieve the flag value use the `useFlag` hook or the `FeatureWrapper`,

#### useFlag
```tsx
// Welcome.tsx
import React, { useCallback } from "react";
import { useFlag } from '@xtreamsrl/react-feature-flags';
import { FeatureFlag } from './shared/feature-flags';

export function Welcome() {  
  const allowSkipTutorial = useFlag(FeatureFlag.AllowSkipTutorial);

  const onClick = useCallback(() => {
    const url = allowSkipTutorial ? '/home' : '/tutorial';
    // ... redirect(url)
  }, [])
  
  return (
    <div>
      <h1>Welcome 👋</h1>
      <button onClick={onClick}>Click Me</button>
    </div>
  );
}
```

#### FeatureWrapper
```tsx
// Welcome.tsx
import React from "react";
import { FeatureWrapper } from '@xtreamsrl/react-feature-flags';
import { FeatureFlag } from './shared/feature-flags';

export function Welcome() {
  return (
    <div>
      <h1>Welcome 👋</h1>
      <FeatureWrapper feature={FeatureFlag.PROFILE}>
        <Profile />
      </FeatureWrapper>
    </div>
  );
}
```

# Who we are
<img align="left" width="80" height="80" src="https://avatars2.githubusercontent.com/u/38501645?s=450&u=1eb7348ca81f5cd27ce9c02e689f518d903852b1&v=4" alt="xtream logo">
A proudly 🇮🇹 software development and data science startup.<br>We consider ourselves a family of talented and passionate people building their own products and powerful solutions for our clients. Get to know us more on <a target="_blank" href="https://xtreamers.io">xtreamers.io</a> or follow us on <a target="_blank" href="https://it.linkedin.com/company/xtream-srl">LinkedIn</a>.

---
_Source: https://npm.io/package/@xtreamsrl/react-feature-flags · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
