Skip to content

Commit

Permalink
feat: add react init sample
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-statsig committed Mar 8, 2024
1 parent 50401ee commit 036074f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable no-console */
import { DJB2 } from '@statsig/client-core';
import { EvaluationsDataAdapter } from '@statsig/precomputed-evaluations';
import {
EvaluationsDataAdapter,
PrecomputedEvaluationsClient,
} from '@statsig/precomputed-evaluations';

import { myStatsigClient } from './sample-precomp-instance';
import { STATSIG_CLIENT_KEY } from '../../Contants';

// <snippet>
// Returns a JSON string from a local file or Statsig Server SDK.
Expand Down Expand Up @@ -35,11 +38,16 @@ function getStatsigBootstrapJson(): string {

// prettier-ignore
export default async function Sample(): Promise<void> {
const dataAdapter = new EvaluationsDataAdapter();

// <snippet>

const user = { userID: 'a-user' };
// </snippet>

const myStatsigClient = new PrecomputedEvaluationsClient(
STATSIG_CLIENT_KEY,
user,
);
const dataAdapter = myStatsigClient.getDataAdapter() as EvaluationsDataAdapter;
// <snippet>

// Pass the bootstrap values to the data adapter
dataAdapter.setDataForUser(user, getStatsigBootstrapJson());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <snippet>
import { PrecomputedEvaluationsClient } from '@statsig/precomputed-evaluations';
import { StatsigProvider, useGate } from '@statsig/react-bindings';

// </snippet>
import { STATSIG_CLIENT_KEY as YOUR_CLIENT_KEY } from '../../Contants';

// prettier-ignore
export default async function Sample(): Promise<void> {
App();
}

// <snippet>
const myStatsigClient = new PrecomputedEvaluationsClient(YOUR_CLIENT_KEY, {
userID: 'a-user',
});

function Content() {
const gate = useGate('a_gate');

return <div>a_gate: {gate.value ? 'Passing' : 'Failing'}</div>;
}

function App() {
return (
<StatsigProvider client={myStatsigClient}>
<Content />
</StatsigProvider>
);
}
// </snippet>

0 comments on commit 036074f

Please sign in to comment.