React SDK
React client side library for Bucket.co
Bucket supports feature toggling, tracking feature usage, requesting feedback on features, and remotely configuring features.
The Bucket React SDK comes with a built-in toolbar which appears on localhost
by default.
Install
Install via npm:
Get started
1. Add the BucketProvider
context provider
BucketProvider
context providerAdd the BucketProvider
context provider to your application:
Example:
2. Create a new feature and set up type safety
Install the Bucket CLI:
Run npx bucket new
to create your first feature!
On the first run, it will sign into Bucket and set up type generation for your project:
[!Note] By default, types will be generated in
gen/features.d.ts
. The defaulttsconfig.json
fileinclude
s this file by default, but if yourtsconfig.json
is different, make sure the file is covered in theinclude
property.
3. Use useFeature(<featureKey>)
to get feature status
useFeature(<featureKey>)
to get feature statusUsing the useFeature
hook from your components lets you toggle features on/off and track feature usage:
Example:
useFeature
can help you do much more. See a full example for useFeature
see below.
Setting user
and company
user
and company
Bucket determines which features are active for a given user
, company
, or otherContext
.
You pass these to the BucketProvider
as props.
If you supply user
or company
objects, they must include at least the id
property otherwise they will be ignored in their entirety.
In addition to the id
, you must also supply anything additional that you want to be able to evaluate feature targeting rules against.
Attributes which are not properties of the user
or company
can be supplied using the otherContext
prop.
Attributes cannot be nested (multiple levels) and must be either strings, numbers or booleans. A number of special attributes exist:
name
-- display name foruser
/company
,email
-- the email of the user,avatar
-- the URL foruser
/company
avatar image.
To retrieve features along with their targeting information, use useFeature(key: string)
hook (described in a section below).
Note that accessing isEnabled
on the object returned by useFeature()
automatically
generates a check
event.
Remote config (beta)
Remote config is a dynamic and flexible approach to configuring feature behavior outside of your app – without needing to re-deploy it.
Similar to isEnabled
, each feature accessed using the useFeature()
hook, has a config
property. This configuration is managed from within Bucket. It is managed similar to the way access to features is managed, but instead of the
binary isEnabled
you can have multiple configuration values which are given to different user/companies.
Get started with Remote config
Update your feature definitions:
key
is mandatory for a config, but if a feature has no config or no config value was matched against the context, the key
will be undefined
. Make sure to check against this case when trying to use the configuration in your application. payload
is an optional JSON value for arbitrary configuration needs.
Note that, similar to isEnabled
, accessing config
on the object returned by useFeature()
automatically
generates a check
event.
<BucketProvider>
component
<BucketProvider>
componentThe <BucketProvider>
initializes the Bucket SDK, fetches features and starts listening for automated feedback survey events. The component can be configured using a number of props:
publishableKey
is used to connect the provider to an environment on Bucket. Find yourpublishableKey
under environment settings in Bucket,company
,user
andotherContext
make up the context that is used to determine if a feature is enabled or not.company
anduser
contexts are automatically transmitted to Bucket servers so the Bucket app can show you which companies have access to which features etc.[!Note] If you specify
company
and/oruser
they must have at least theid
property, otherwise they will be ignored in their entirety. You should also supply anything additional you want to be able to evaluate feature targeting against,fallbackFeatures
: A list of strings which specify which features to consider enabled if the SDK is unable to fetch features. Can be provided in two formats:timeoutMs
: Timeout in milliseconds when fetching features from the server,staleWhileRevalidate
: If set totrue
, stale features will be returned while refetching features in the background,expireTimeMs
: If set, features will be cached between page loads for this duration (in milliseconds),staleTimeMs
: Maximum time (in milliseconds) that stale features will be returned ifstaleWhileRevalidate
is true and new features cannot be fetched.loadingComponent
lets you specify an React component to be rendered instead of the children while the Bucket provider is initializing. If you want more control over loading screens,useFeature()
returnsisLoading
which you can use to customize the loading experience:enableTracking
: Set tofalse
to stop sending tracking events and user/company updates to Bucket. Useful when you're impersonating a user (defaults totrue
),apiBaseUrl
: Optional base URL for the Bucket API. Use this to override the default API endpoint,appBaseUrl
: Optional base URL for the Bucket application. Use this to override the default app URL,sseBaseUrl
: Optional base URL for Server-Sent Events. Use this to override the default SSE endpoint,debug
: Set totrue
to enable debug logging to the console,toolbar
: Optional configuration for the Bucket toolbar,feedback
: Optional configuration for feedback collection
Hooks
useFeature()
useFeature()
Returns the state of a given feature for the current context. The hook provides type-safe access to feature flags and their configurations.
useTrack()
useTrack()
useTrack()
lets you send custom events to Bucket. Use this whenever a user uses a feature. Create features in Bucket based off of these events to analyze feature usage.
Returns a function to send custom events to Bucket. Use this whenever a user uses a feature. These events can be used to analyze feature usage and create new features in Bucket.
useRequestFeedback()
useRequestFeedback()
Returns a function that lets you open up a dialog to ask for feedback on a specific feature. This is useful for collecting targeted feedback about specific features.
useRequestFeedback()
returns a function that lets you open up a dialog to ask for feedback on a specific feature.
See Automated Feedback Surveys for how to do this automatically, without code.
When using the useRequestFeedback
you must pass the feature key to requestFeedback
.
The example below shows how to use position
to ensure the popover appears next to the "Give feedback!" button.
See the Feedback Documentation for more information on requestFeedback
options.
useSendFeedback()
useSendFeedback()
Returns a function that lets you send feedback to Bucket. This is useful if you've manually collected feedback through your own UI and want to send it to Bucket.
useUpdateUser()
, useUpdateCompany()
and useUpdateOtherContext()
useUpdateUser()
, useUpdateCompany()
and useUpdateOtherContext()
These hooks return functions that let you update the attributes for the currently set user, company, or other context. Updates to user/company are stored remotely and affect feature targeting, while "other" context updates only affect the current session.
Note: To change the user.id
or company.id
, you need to update the props passed to BucketProvider
instead of using these hooks.
useClient()
useClient()
Returns the BucketClient
used by the BucketProvider
. The client offers more functionality that
is not directly accessible thorough the other hooks.
Content Security Policy (CSP)
See CSP for info on using Bucket React SDK with CSP
License
MIT License
Copyright (c) 2025 Bucket ApS
Last updated
Was this helpful?