-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved feature flags quickstart with plugin instructions
- Loading branch information
Oscar Söderlund
committed
May 14, 2024
1 parent
c69faae
commit 9ce367e
Showing
4 changed files
with
179 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,8 +91,99 @@ Now, let's integrate this into our app! | |
|
||
##### Example code | ||
<Tabs> | ||
|
||
<TabItem value="reactjs" label="ReactJS" default> | ||
|
||
1. Install the Nblocks react plugin | ||
|
||
```bash | ||
npx @nebulr-group/nblocks-cli setup-react | ||
``` | ||
|
||
2. Add the NblocksProvider | ||
Add the `NblocksProvider` to your top most component, e.g. the App component and wrap the rest of your app as children. | ||
|
||
```tsx | ||
import { NblocksProvider } from '@nebulr-group/nblocks-react'; | ||
|
||
export default function App() { | ||
return ( | ||
<NblocksProvider config={{ appId: 'XXX' /* Replace this with your own APP ID */ }}> | ||
... App components ... | ||
</NblocksProvider> | ||
); | ||
} | ||
``` | ||
|
||
3. Use the FeatureFlag Component | ||
|
||
Imagine you have a component that looks something like this: | ||
|
||
```tsx | ||
// We just want to render this for premium customers | ||
<span>Premium content</span> | ||
|
||
// We just want to render this for admins | ||
<a href="/beta">Button to admin features</a> | ||
|
||
// We just want to render this if we're doing a release | ||
<h1>We're currently doing a release and will be back soon</h1> | ||
``` | ||
|
||
Now you can use the `FeatureFlagComponent` from the plugin to conditionally show these components in your React app. | ||
|
||
```tsx | ||
import { FeatureFlagComponent } from '@nebulr-group/nblocks-react'; | ||
|
||
// highlight-start | ||
<FeatureFlagComponent flag="premium-features"> | ||
// highlight-end | ||
<span>Premium content</span> | ||
// highlight-start | ||
</FeatureFlagComponent> | ||
<FeatureFlagComponent flagKey="admin-features"> | ||
// highlight-end | ||
<a href="/beta">Button to admin features</a> | ||
// highlight-start | ||
</FeatureFlagComponent> | ||
<FeatureFlagComponent flagKey="release-announcement"> | ||
// highlight-end | ||
<h1>We're currently doing a release and will be back soon</h1> | ||
// highlight-start | ||
</FeatureFlagComponent> | ||
// highlight-end | ||
``` | ||
|
||
4. Provide the user context | ||
|
||
To attach context information to the flags when they get evaluated use `setContext` method from the `useFlags()` hook. | ||
This is especially useful to set during your login process when the user is identified. | ||
|
||
```tsx | ||
import { useFlags } from '@nebulr-group/nblocks-react'; | ||
|
||
const { setContext } = useFlags(); | ||
|
||
setContext({ | ||
user: { | ||
id: "[email protected]", | ||
... | ||
} | ||
}) | ||
``` | ||
|
||
:::tip | ||
|
||
When calling `setContext()` all your FeatureFlagComponents will reload automatically. | ||
|
||
You can also provide an inital flags context to `NblocksProvider`. | ||
|
||
::: | ||
|
||
</TabItem> | ||
|
||
<TabItem value="reactjs-vanilla" label="ReactJS (vanilla)"> | ||
|
||
Imagine you have a component that looks something like this: | ||
|
||
```tsx | ||
|
@@ -159,6 +250,12 @@ This object could be stored during your login process when the user is identifie | |
|
||
::: | ||
|
||
:::tip | ||
|
||
Instead of making multiple requests for each flag you can evaluate all flags in bulk once for better performance. See the [API reference](https://nebulr-group.github.io/nblocks-api-docs/#evaluate-flags-in-bulk) | ||
|
||
::: | ||
|
||
Now we can use this component anywhere in your app to conditionally show content like pages or buttons in our React app. | ||
Like this, where the changes are highlighted: | ||
|
||
|
@@ -254,6 +351,12 @@ This object could be stored during your login process when the user is identifie | |
|
||
::: | ||
|
||
:::tip | ||
|
||
Instead of making multiple requests for each flag you can evaluate all flags in bulk once for better performance. See the [API reference](https://nebulr-group.github.io/nblocks-api-docs/#evaluate-flags-in-bulk) | ||
|
||
::: | ||
|
||
Now we can use this component anywhere in your app to conditionally show content like pages or buttons in our app. | ||
Like this, where the changes are highlighted: | ||
|
||
|
@@ -360,6 +463,12 @@ This object could be stored during your login process when the user is identifie | |
|
||
::: | ||
|
||
:::tip | ||
|
||
Instead of making multiple requests for each flag you can evaluate all flags in bulk once for better performance. See the [API reference](https://nebulr-group.github.io/nblocks-api-docs/#evaluate-flags-in-bulk) | ||
|
||
::: | ||
|
||
Now we can use the `evaluateFlag` method anywhere in your app to conditionally run operations, such as the new model. | ||
Like in this `ModelServlet`: | ||
|
||
|
@@ -440,9 +549,3 @@ You should see the content that was set with the flag `beta-feature`. Now you ca | |
|
||
|
||
**That's it, your now done with this tutorial** | ||
|
||
:::tip | ||
|
||
Instead of making multiple requests for each flag you can evaluate all flags in bulk once for better performance. See the [API reference](https://nebulr-group.github.io/nblocks-api-docs/#evaluate-flags-in-bulk) | ||
|
||
::: |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.