Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: How to Implement Feature Flags in React using Unleash #5292

Closed
rishi-raj-jain opened this issue Nov 7, 2023 · 2 comments
Closed

docs: How to Implement Feature Flags in React using Unleash #5292

rishi-raj-jain opened this issue Nov 7, 2023 · 2 comments
Assignees
Labels
docs Documentation

Comments

@rishi-raj-jain
Copy link

rishi-raj-jain commented Nov 7, 2023

Outline 👇🏻


How to Implement Feature Flags in React using Unleash

Some additional information

In this tutorial, you will learn how to use feature flags in a [React](https://reactjs.org/) application that shows different content based on the current time and set the time new content shall be shown, using Unleash. We will use the @unleash/proxy-client-react and unleash-proxy-client packages, which provides easy integration of Unleash feature flags in a React application.

What we’ll be using

Mention:

  • Tech (It's purpose)
  • Tech (It's purpose)
  • Tech (It's purpose)

What you’ll need

  • Tech (It's purpose)
  • Tech (It's purpose)

Setting up the project

To set up, just clone the app repo and follow this tutorial to learn everything that's in it. To fork the project, run:

git clone https://github.com/rishi-raj-jain/repo-slug
# Some additional information

Scaffolding a React app

Creating an React app is as easy as a single command:

# command

Setup Unleash

  1. Run the following commands in the terminal to fetch the docker-compose.yml for creating an Unleash instance:
wget getunleash.io/docker-compose.yml
docker-compose up -d

This will start Unleash in the background. Once Unleash is running, you can access it at http://localhost:4242/.

  1. Now use the default credentials to log into the instance:
Username: admin
Password: unleash4all

Exploring Unleash's currentTime strategy constraint

To display different contents based on the time it's being accessed, we'll be use an existing strategy constraint named currentTime in Unleash. This will help us decide whether the new content shall be shown, and use that flag in our app.

Screenshots

Create a New Feature

Create a new feature flag in your Unleash instance named timebased-content.

Steps to use the exisiting constraint currentTime

Screenshots

Integrating Unleash in React app

Installation

To get started with React and Unleash, you need to install @unleash/proxy-client-react and unleash-proxy-client packages as dependencies.

You can run the following commands in your terminal to do this:

npm install @unleash/proxy-client-react unleash-proxy-client

Set up Environment Variables

By default, the following values are setup in your local Unleash instance

# .env

UNLEASH_API_URL="http://localhost:4242/api"
UNLEASH_AUTHORIZATION_KEY="default:development.unleash-insecure-api-token"

Initialise Unleash SDK

Some additional information

// some code

Use Unleash SDK to fetch the feature flag value

Some additional information

// some code

Scenarios

Some additional information

Screenshots

Revert: Scenarios

Some additional information

Screenshots

Using Unleash in Production

To setup Unleash for production, please follow the steps below:

  1. Self-host Unleash, or run an instance on [Unleash Cloud](https://www.getunleash.io/pricing).
  2. Get an [API key](/reference/api-tokens-and-client-keys) from the Unleash dashboard.
  3. Store the API key in your Environment Variables of your hosting, which secures it and makes it accessible in your code.

Conclusion

Some additional information

@Tymek Tymek moved this from New to In Progress in Issues and PRs Nov 7, 2023
@Tymek Tymek added the docs Documentation label Nov 7, 2023
@rishi-raj-jain
Copy link
Author

Timed Based Content demo with Unleash and React

React.Timed.Content.Unleash.mp4

@ivarconr
Copy link
Member

ivarconr commented Jan 9, 2024

we have a guide for react written by @nnennandukwe here: https://docs.getunleash.io/feature-flag-tutorials/react

@ivarconr ivarconr closed this as completed Jan 9, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Issues and PRs Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation
Projects
None yet
Development

No branches or pull requests

4 participants