@schematichq/schematic-react 0.2.0-rc.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +86 -0
- package/dist/schematic-react.cjs.js +909 -12566
- package/dist/schematic-react.d.ts +0 -2988
- package/dist/schematic-react.esm.js +892 -12581
- package/package.json +16 -30
package/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# schematic-react
|
2
|
+
|
3
|
+
`schematic-react` is a client-side React library for [Schematic](https://schematichq.com) which provides hooks to track events, check flags, and more. `schematic-react` provides the same capabilities as [schematic-js](https://github.com/schematichq/schematic-js/tree/main/js), for React apps.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
```bash
|
8
|
+
npm install @schematichq/schematic-react
|
9
|
+
# or
|
10
|
+
yarn add @schematichq/schematic-react
|
11
|
+
# or
|
12
|
+
pnpm add @schematichq/schematic-react
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
You can use the `SchematicProvider` to wrap your application and provide the Schematic instance to all components:
|
18
|
+
|
19
|
+
```tsx
|
20
|
+
import { SchematicProvider } from "@schematichq/schematic-react";
|
21
|
+
|
22
|
+
ReactDOM.render(
|
23
|
+
<SchematicProvider publishableKey="your-publishable-key">
|
24
|
+
<App />
|
25
|
+
</SchematicProvider>,
|
26
|
+
document.getElementById("root"),
|
27
|
+
);
|
28
|
+
```
|
29
|
+
|
30
|
+
To set the user context for events and flag checks, you can use the `identify` function provided by the `useSchematicEvents` hook:
|
31
|
+
|
32
|
+
```tsx
|
33
|
+
import { useSchematicEvents } from "@schematichq/schematic-react";
|
34
|
+
|
35
|
+
const MyComponent = () => {
|
36
|
+
const { identify } = useSchematicEvents();
|
37
|
+
|
38
|
+
useEffect(() => {
|
39
|
+
identify({
|
40
|
+
keys: { id: "my-user-id" },
|
41
|
+
company: {
|
42
|
+
keys: { id: "my-company-id" },
|
43
|
+
traits: { location: "Atlanta, GA" },
|
44
|
+
},
|
45
|
+
});
|
46
|
+
}, []);
|
47
|
+
|
48
|
+
return <div>My Component</div>;
|
49
|
+
};
|
50
|
+
```
|
51
|
+
|
52
|
+
Once you've set the context with `identify`, you can track events:
|
53
|
+
|
54
|
+
```tsx
|
55
|
+
import { useSchematicEvents } from "@schematichq/schematic-react";
|
56
|
+
|
57
|
+
const MyComponent = () => {
|
58
|
+
const { track } = useSchematicEvents();
|
59
|
+
|
60
|
+
useEffect(() => {
|
61
|
+
track({ event: "query" });
|
62
|
+
}, []);
|
63
|
+
|
64
|
+
return <div>My Component</div>;
|
65
|
+
};
|
66
|
+
```
|
67
|
+
|
68
|
+
To check a flag, you can use the `useSchematicFlags` hook:
|
69
|
+
|
70
|
+
```tsx
|
71
|
+
import { useSchematicFlag } from "@schematichq/schematic-react";
|
72
|
+
|
73
|
+
const MyComponent = () => {
|
74
|
+
const isFeatureEnabled = useSchematicFlag("my-flag-key");
|
75
|
+
|
76
|
+
return isFeatureEnabled ? <Feature /> : <Fallback />;
|
77
|
+
};
|
78
|
+
```
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
MIT
|
83
|
+
|
84
|
+
## Support
|
85
|
+
|
86
|
+
Need help? Please open a GitHub issue or reach out to [support@schematichq.com](mailto:support@schematichq.com) and we'll be happy to assist.
|