Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@spotify-confidence/integration-react
Advanced tools
![](https://img.shields.io/badge/lifecycle-beta-a0c3d2.svg)
This is a helper package to use OpenFeature
in React and ReactNative. It is not official OpenFeature and relies on behaviour from
the ConfidenceWebProvider specifically.
To add the packages to your dependencies run:
yarn add @openfeature/web-sdk @spotify-confidence/openfeature-web-provider @spotify-confidence/integration-react
setProvider
makes the Provider launch a network request to initialize the flags. In cases of success the ProviderEvents.Ready
event will be emitted. In cases of failure of the network request, the ProviderEvent.Error
event will be emitted. The
ProviderEvents events will be emitted only when we are done with the network request, either a successful or a failed
network response. If the network response failed, default values will be returned on flag evaluation, if the network
request is successful we update the flags and then emit ProviderEvents.Ready
.
import React, { useEffect } from 'react';
import { createConfidenceWebProvider } from '@spotify-confidence/openfeature-web-provider';
import { OpenFeature, OpenFeatureAPI } from '@openfeature/web-sdk';
import { useStringValue } from '@spotify-confidence/integration-react';
const provider = createConfidenceWebProvider({
clientSecret: 'mysecret',
region: 'eu',
fetchImplementation: window.fetch.bind(window),
});
OpenFeature.setProvider(provider);
const App = () => {
useEffect(() => {
OpenFeature.setContext({
targetingKey: 'myTargetingKey',
});
}, []);
return (
<React.Suspense fallback={<Loading />}>
<InnerComponent />
</React.Suspense>
);
};
function InnerComponent() {
const str = useStringValue('flag.some-string', 'default');
return <p>{str}</p>;
}
To use a custom client you can wrap your components in the OpenFeatureContextProvider
component.
import { OpenFeatureContextProvider } from '@spotify-confidence/integration-react';
const App = () => {
return (
<OpenFeatureContextProvider name={'my-client-name'}>
<React.Suspense fallback={<Loading />}>
<InnerComponent />
</React.Suspense>
</OpenFeatureContextProvider>
);
};
Notes:
React.Suspense
is used to show users a loading state whilst the flags are pending, to limit flickering experiences for users.import { useStringValue } from '@spotify-confidence/integration-react';
const str: string = useStringValue('flagKey', 'default');
import { useStringDetails } from '@spotify-confidence/integration-react';
import { ResolutionDetails } from '@openfeature/web-sdk';
const details: ResolutionDetails<string> = useStringDetails('flagKey', 'default');
import { useNumberValue } from '@spotify-confidence/integration-react';
const num: number = useNumberValue('flagKey', 0);
import { useNumberDetails } from '@spotify-confidence/integration-react';
import { ResolutionDetails } from '@openfeature/web-sdk';
const details: ResolutionDetails<number> = useNumberDetails('flagKey', 0);
import { useBooleanValue } from '@spotify-confidence/integration-react';
const bool: boolean = useBooleanValue('flagKey', false);
import { useBooleanDetails } from '@spotify-confidence/integration-react';
import { ResolutionDetails } from '@openfeature/web-sdk';
const details: ResolutionDetails<boolean> = useBooleanDetails('flagKey', false);
import { useObjectValue } from '@spotify-confidence/integration-react';
const struct: { val: string } = useObjectValue('flagKey', { val: 'default' });
import { useObjectDetails } from '@spotify-confidence/integration-react';
import { ResolutionDetails } from '@openfeature/web-sdk';
const details: ResolutionDetails<{ val: string }> = useObjectDetails('flagKey', { val: 'default' });
In Next13's server components using a dynamic paradigm OpenFeature sdk is recommended. This means setting up a Confidence server provider, see docs. Then you can use the async methods on an OpenFeature Client in your server component.
There are two options for usage of OpenFeature's Clients in Next's Client Components.
Option #1 is very simple to implement and will work for most cases.
Option #2 is more complex and requires the OpenFeature global API to be configured in the client bundle (under a
'use client'
directive), and the Confidence Web Provider, OpenFeature Context and OpenFeature's provider must be
set only in the browser. This will require making a component with a client boundary and checking if you're on the sever
either by using a useEffect Hook, or by checking the typeof window
.
In Next12 using a dynamic paradigm OpenFeature sdk is recommended. This means setting up a
Confidence server provider, see docs. Using this OpenFeature client in the
getServerSideProps
method to pass the retrieved flag values from the server to the client.
FAQs
![](https://img.shields.io/badge/lifecycle-deprecated-a0c3d2.svg)
The npm package @spotify-confidence/integration-react receives a total of 5 weekly downloads. As such, @spotify-confidence/integration-react popularity was classified as not popular.
We found that @spotify-confidence/integration-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.