https://github.com/foo-software/react-feature-toggle
A feature toggle for React
https://github.com/foo-software/react-feature-toggle
Last synced: 3 months ago
JSON representation
A feature toggle for React
- Host: GitHub
- URL: https://github.com/foo-software/react-feature-toggle
- Owner: foo-software
- Created: 2020-05-09T15:27:19.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T05:25:30.000Z (about 3 years ago)
- Last Synced: 2024-04-24T16:24:21.027Z (almost 2 years ago)
- Language: JavaScript
- Size: 1.34 MB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# React Feature Toggle
> `@foo-software/react-feature-toggle`
A feature toggle for React. Feature toggles (otherwise known as feature flags or feature switches) are a software development technique that provides a way of turning functionality or display on and off during runtime, without deploying new code. This allows for more control and experimentation over the full lifecycle of features. Feature toggles are a best practice in DevOps, often occurring within distributed version control systems.
## Setup
This library depends on a [feature toggle account with Foo](https://www.foo.software). Follow the steps below to get started.
1. Choose an account from the [plans page](https://www.foo.software/pricing) and register.
2. Navigate to the account page and click on the API tab. Make note of the account ID.
3. Create a feature toggle in the dashboard. If needed [follow the docs](https://www.foo.software/toggle-tools-how-to-create-a-feature-toggle/)!
4. Navigate to the toggle detail page through the dashboard screen and make note of the "Environment API Name" and "Toggle API Name".
At this point you should have three items noted: account ID, environment name, and toggle name.
## Usage
You'll need to use a "provider" to setup configuration at the application level. You could do this in `App.js` for example.
> App.js
```jsx
import React from 'react';
// other imports...
import { ToggleProvider } from '@foo-software/react-feature-toggle';
// this is just an example, but these values can come from anywhere
// you're comfortable with
const {
REACT_APP_TOGGLE_ACCOUNT_ID,
REACT_APP_TOGGLE_ENVIRONMENT,
} = process.env;
export default () => {
// things...
return (
{/* all the things here */}
)
};
```
With the above you can use the `useToggle` hook like so.
> Example.js
```jsx
import { useToggle } from '@foo-software/react-feature-toggle';
export default () => {
const { isOn } = useToggle({ toggleName: 'example' });
return (
<>
{isOn && (
My example feature.
)}
Some other thing.
>
);
};
```
## `ToggleProvider`: Props
Name
Type
Required
Description
accountId
string
yes
The account ID of your Toggle Tools account as explained in the setup section.
environmentName
string
yes
The environment the toggle belongs to by name as explained in the setup section.
fetch
function
no
By default React Feature Toggle will use the Fetch Web API to fetch toggle state from Toggle Tools API. This prop can be used to override by providing preferable fetch function. It should share the same API.
children
node
yes
The node to be rendered by the provider. This is similar to any standard provider component.
## `useToggle`
Accepts an object argument with a `toggleName` property. `toggleName` should be the value described in the [setup section](#setup).
#### `useToggle`: Return Object
Name
Type
Description
isOn
boolean
Signifies whether the toggle is toggled on.
status
oneOf([null, "pending", "fulfilled", "rejected"])
Fetch status of the toggle.
## `ToggleContext`
A React context set by `ToggleProvider`. It holds values from props passed to `ToggleProvider` with the addition of a value named `toggles`. `toggles` is a collection of all toggles set in the current runtime. Below is an example.
```json
{
"myToggle1": {
"isOn": false,
"status": "fulfilled"
},
"myToggle2": {
"isOn": null,
"status": "rejected"
}
}
```