the React (with TypeScript support) SDK for Molasses. It allows you to evaluate a user's status for a feature. It also helps simplify logging events for A/B testing.
react-molasses
offers a provider with hooks and with
support. Once initialized, it takes microseconds to evaluate if a user is active. react-molasses
does rely on molasses-js
as a client.
npm install @molassesapp/molasses-js react-molasses
yarn add @molassesapp/molasses-js react-molasses
To Require:
1import { MolassesClient } from "@molassesapp/molasses-js";2import { MolassesProvider } from "react-molasses";
Initialize the same way you would with molasses-js
, but wrap your main application in the MolassesProvider
1// Initialize with your API Key2const client = new MolassesClient({3APIKey: "testapikey",4});56// Then init which is a promise7client.init().then(() => {8const AppContainer = () => {9return (10<MolassesProvider client={client}>11<Router>12<App />13</Router>14</MolassesProvider>15);16};1718ReactDOM.render(<AppContainer />, document.getElementById("root"));19});
Once it's initialized, you can then use Molasses in any component using React's context api. We've created a set of hooks and components to make this easier for you.
<MolassesProvider client={client}>
Wraps any children and provides the MolassesClient
in the React.context
. Molasses must be initialized before it can be used.
Prop | required | type | description |
---|---|---|---|
client | required | MolassesClient | the initialized MolassesClient to pass into the react context |
<Feature name="NAME" user={user}>
A component to evaluate a Feature by name. It can be used in several different ways, declarative using children, function based and using the render
prop.
Prop | required | type | description |
---|---|---|---|
name | required | string | The name of the feature to be evaluated |
user | optional | User | The user who to evaluate against |
render | optional | `Function | JSX.Element` |
render | optional | `Function | JSX.Element` |
1const Component = (props) => (2<Feature name="NEW_CHECKOUT" user={user}>3<div>I'll render if NEW_CHECKOUT is active for that user</div>4</Feature>5);67const ComponentB = (props) => (8<Feature9name="NEW_CHECKOUT"10user={user}11render={(isActive: boolean) => {12return isActive ? (13<span>I'll render if I'm active</span>14) : (15<span>I won't</span>16);17}}18/>19);2021const ComponentC = (props) => (22<Feature name="NEW_CHECKOUT" user={user}>23{(isActive: boolean) => {24return isActive ? (25<span>I'll render if I'm active</span>26) : (27<span>I won't</span>28);29}}30</Feature>31);
useFeature(name: string, user: User): boolean
A hook to determine if a given feature is active for a User
. Uses the Molasses context under the hood
useMolasses(): MolassesClient
A hook to expose the Molasses client to a component
withMolasses(Component: React Component)
A HOC that passes the MolassesClient
as the prop molasses
to the component
1const comp = (props) => {2const isActive = props.molasses.isActive("FOO_TEST");3return isActive ? <h1>active</h1> : <h1>off</h1>;4};5const Component = withMolasses(comp);