Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@spotify-confidence/react
Advanced tools
This package contains helper functionality to make the Confidence SDK work well in a React environment.
To add the packages to your dependencies run:
yarn add @spotify-confidence/react
The Confidence React integration has a Provider that needs to be initialized. It accepts a Confidence instance and should wrap your component tree.
import { Confidence } from '@spotify-confidence/sdk';
const confidence = Confidence.create({
clientSecret: 'mysecret',
region: 'eu',
environment: 'client',
timeout: 1000,
});
function App() {
return (
<ConfidenceProvider confidence={confidence}>
<React.Suspense fallback={<p>Loading... </p>}>
<MyComponent />
</React.Suspense>
</ConfidenceProvider>
);
}
Anywhere in the sub-tree under the ConfidenceProvider
you can now access the confidence instance with the useConfidence()
hook. The hook actually returns an instance of ConfidenceReact
which is a wrapper around the normal Confidence
API with some slight adaptations to integrate better with React. You can read more about the differences in the following sections.
The ConfidenceReact
instance supports the standard context API. Additionally, the following wrapper component can be used to wrap a sub tree with additional context data.
<ConfidenceProvider.WithContext context={{ user_name: 'John Doe' }}>
<UserDetails />
</ConfidenceProvider.WithContext>
Flags are accessed with a set of hooks exported from @spotify-confidence/react
useFlag(flagName, defaultValue)
will return the flag value or default.useEvaluateFlag(flagName, defaultValue)
will return more details about the flag evaluation, together with the value.Alternatively the ConfidenceReact
instance has the same hooks as methods on the instance itself, remember though, that these instance methods are hooks and the normal rules for hooks apply.
Both of the flag hooks integrate with the React Suspense API so that the suspense fallback will be shown until flag values are available. It is therefore important to wrap any component using the above hooks in a suspense boundary.
The hooks are also reactive so that if the context changes, any components using the hooks will be re-rendered. As dependent components are re-rendered as soon as the context changes, one might expect that would again trigger the suspense boundary while the flag values are resolved. That is normally not the case however as the ConfidenceReact.setContext
method is by default wrapped in a React Transition. If you would rather manage the transition logic yourself (with for example React.useTransition()), or if you want to always trigger a suspense fallback to never show stale values, you can turn off the default behavior by calling ConfidenceReact.setContext({...}, { transition:false })
.
If the hooks can't resolve the flag values withing the timeout specified on the Confidence instance, they will instead return the default value.
The event tracking API is available on the Confidence instance as usual. See the SDK Readme for details.
const confidence = useConfidence();
confidence.track('my-event-name', { my_data: 4 });
FAQs
Unknown package
The npm package @spotify-confidence/react receives a total of 126 weekly downloads. As such, @spotify-confidence/react popularity was classified as not popular.
We found that @spotify-confidence/react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.