Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
react-tiny-feature-switch
Advanced tools
🚗 A feature-packed, tiny, fast (~900B) React Feature Toggle
This project uses React Hooks, so you need at least React version 16.8
npm i react-tiny-feature-switch
or yarn add react-tiny-feature-switch
The left side of the example is the rendered code. The right side is the actual code used to render the left side. https://dericgw.github.io/react-tiny-feature-switch
Here is the basic gist of how to use it...
// features.json
// The shape of the features object must be like so... [string]: boolean
{
"avatar": false,
"profile": true
}
// index.js
import { Features } from 'react-tiny-feature-switch'
import features from './features.json'; // Could be a JS file
const App = () => (
<Features features={features}>
<OtherAppStufff />
</Features>
);
// profile.js
import { FeatureSwitch, Else } from 'react-tiny-feature-switch';
const Profile = () => (
<div>
// See if the "avatar" feature is available
<FeatureSwitch feature="avatar">
<img src="/users-profile-pic.png" alt="Profile Pic" />
// If the "avatar" is not available, then render something Else
<Else>
<img src="/generic-avatar-image.png" alt="Avatar" />
</Else>
</FeatureSwitch>
</div>
);
<Features />
This is the Provider component that uses React Context in order to pass the props down to the other
components. It only has one prop, features
, which takes an object of features whose key is the
feature name and value is a boolean. This component should be close to the root component so that
all of the child components have access to the provided props.
Name | Type | Default Value | Required | Description |
---|---|---|---|---|
features | Object { [string]: boolean } | {} | true | This is an object of features that the app will have and it should be in the form of { featureName: true } (or, false if the feature is disabled) |
<FeatureSwitch />
This component will wrap the features that are being worked on and either show them or hide them if
the feature is enabled, or disabled. The <FeatureSwitch />
component accepts two props, a feature
prop and children
props. The feature
prop is a string
or string[]
(array of strings). If the
string that is passed in is true
in the features
object, which is passed into the <Features />
component, the child components will be displayed.
If an array of strings are passed in, they will both need to features that resolve to true
in order
for the feature to be enabled. This could be useful for versioning the app. For instance, if your
are working on v1.1
, then all of the features that are related to v1.1
could be set to true
in
the features
object. Also in that features
object, there could be a v1.1
feature that is set to
true
as well. Now, you do something like this: <FeatureSwitch feature={['profile', 'v1.1']} />
.
If you want to hide all of the v1.1
features (maybe you need to do a hotfix), you can set the
v1.1: false
in the features
object, and none of the v1.1
features will be displayed.
The children
prop is any React type node.
Name | Type | Default Value | Required | Description |
---|---|---|---|---|
feature | string | true | This is the feature that will be either "on" or "off", depending on whether it is true or false in the features object of the <Features /> component. | |
children | React.ReactNode | true | This is the component or components that will be rendered if the feature is enabled. |
<Else />
Sometimes it will be necessary to render parts of the UI when a feature is disabled - this is the job
of the <Else />
component. The <Else />
component should be a child to the <FeatureSwitch />
component. Check it out...
<FeatureSwitch feature="avatar">
<img src="/users-profile-pic.png" alt="Profile Pic" />
// If the "avatar" is not available, then render something Else
<Else>
<img src="/generic-avatar-image.png" alt="Avatar" />
</Else>
</FeatureSwitch>
NOTE: The
<Else />
component will not work if it is not a child of the<FeatureSwitch />
component
Name | Type | Default Value | Required | Description |
---|---|---|---|---|
children | React.ReactNode | true | This is the component or components that will be rendered if the parent feature , in the <FeatureSwitch /> , is disabled. |
<FeatureToggler />
The <FeatureToggler />
is a helper component that easily allows a dev or QA engineer to toggle
features on and off. Just add the <FeatureToggler />
component to the app and you will notice a
div (bottom, right corner) with a checkbox for each feature is rendered in the app. The value of the
checkbox will reflect the features
object of the <Features />
component. Here is what it looks
like:
NOTE: It is a good idea to conditionally render the
<FeatureToggler />
component so that your production build does not include it.
The <FeatureToggler />
accepts one props - alwaysShow
, which is a boolean. If this is set to
true
, the <FeatureToggler />
will always be displayed. If this prop is not present, or set to
false
, then the <FeatureToggler />
will only be shown if a URL Query param of featureToggler
is
present in the URL, e.g., http://localhost?featureToggler=true
You can pass query params in the URL to override a feature's state (enabled or disabled). If you
want to enable a feature, you can pass true
or 1
. Anything else will evaluate to false
. For
instance:
http://localhost?firstName=true&lastName=1&profile=false&address=foobar
In the case above, here is the final evaluation:
firstName: true
lastName: true
profiletName: false
address: false
NOTE: The URL Query param values will override what is passed in to the
features
object.
More on this very soon...
Open up an issue if you find one. If you can provide a reproduction, then please do. You can use codesandbox.io for this.
FAQs
A feature-packed, tiny (~650B) React Feature Toggle
The npm package react-tiny-feature-switch receives a total of 4 weekly downloads. As such, react-tiny-feature-switch popularity was classified as not popular.
We found that react-tiny-feature-switch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.