@posthog/react 1.10.1 → 1.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -197
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -1,204 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PostHog React package
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Please see the main [PostHog docs](https://posthog.com/docs).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
SDK usage examples and code snippets live in the official documentation so they stay up to date.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Documentation
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
npm install @posthog/react posthog-js
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
### Setting up the Provider
|
|
16
|
-
|
|
17
|
-
Wrap your application with `PostHogProvider` to make the PostHog client available throughout your app:
|
|
18
|
-
|
|
19
|
-
```tsx
|
|
20
|
-
import { PostHogProvider } from '@posthog/react'
|
|
21
|
-
|
|
22
|
-
function App() {
|
|
23
|
-
return (
|
|
24
|
-
<PostHogProvider apiKey="<YOUR_PROJECT_API_KEY>" options={{ api_host: 'https://us.i.posthog.com' }}>
|
|
25
|
-
<YourApp />
|
|
26
|
-
</PostHogProvider>
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Or pass an existing PostHog client instance:
|
|
32
|
-
|
|
33
|
-
```tsx
|
|
34
|
-
import posthog from 'posthog-js'
|
|
35
|
-
import { PostHogProvider } from '@posthog/react'
|
|
36
|
-
|
|
37
|
-
// Initialize your client
|
|
38
|
-
posthog.init('<YOUR_PROJECT_API_KEY>', { api_host: 'https://us.i.posthog.com' })
|
|
39
|
-
|
|
40
|
-
function App() {
|
|
41
|
-
return (
|
|
42
|
-
<PostHogProvider client={posthog}>
|
|
43
|
-
<YourApp />
|
|
44
|
-
</PostHogProvider>
|
|
45
|
-
)
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Hooks
|
|
50
|
-
|
|
51
|
-
#### usePostHog
|
|
52
|
-
|
|
53
|
-
Access the PostHog client instance to capture events, identify users, etc.
|
|
54
|
-
|
|
55
|
-
```tsx
|
|
56
|
-
import { usePostHog } from '@posthog/react'
|
|
57
|
-
|
|
58
|
-
function MyComponent() {
|
|
59
|
-
const posthog = usePostHog()
|
|
60
|
-
|
|
61
|
-
const handleClick = () => {
|
|
62
|
-
posthog.capture('button_clicked', { button_name: 'signup' })
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return <button onClick={handleClick}>Sign Up</button>
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
#### useFeatureFlagEnabled
|
|
70
|
-
|
|
71
|
-
Check if a feature flag is enabled for the current user.
|
|
72
|
-
|
|
73
|
-
```tsx
|
|
74
|
-
import { useFeatureFlagEnabled } from '@posthog/react'
|
|
75
|
-
|
|
76
|
-
function MyComponent() {
|
|
77
|
-
const isEnabled = useFeatureFlagEnabled('new-feature')
|
|
78
|
-
|
|
79
|
-
if (isEnabled) {
|
|
80
|
-
return <NewFeature />
|
|
81
|
-
}
|
|
82
|
-
return <OldFeature />
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
By default the hook returns `boolean | undefined`, where `undefined` means the flags haven't loaded yet or the flag is absent. Pass a `defaultValue` to use instead of `undefined` in those cases — the return type then narrows to `boolean`:
|
|
87
|
-
|
|
88
|
-
```tsx
|
|
89
|
-
// false until proven enabled
|
|
90
|
-
const isEnabled = useFeatureFlagEnabled('new-feature', false)
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
#### useFeatureFlagVariantKey
|
|
94
|
-
|
|
95
|
-
Get the variant key for a multivariate feature flag.
|
|
96
|
-
|
|
97
|
-
```tsx
|
|
98
|
-
import { useFeatureFlagVariantKey } from '@posthog/react'
|
|
99
|
-
|
|
100
|
-
function MyComponent() {
|
|
101
|
-
const variant = useFeatureFlagVariantKey('experiment-flag')
|
|
102
|
-
|
|
103
|
-
if (variant === 'control') {
|
|
104
|
-
return <ControlVariant />
|
|
105
|
-
}
|
|
106
|
-
if (variant === 'test') {
|
|
107
|
-
return <TestVariant />
|
|
108
|
-
}
|
|
109
|
-
return null
|
|
110
|
-
}
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
#### useFeatureFlagPayload
|
|
114
|
-
|
|
115
|
-
Get the payload associated with a feature flag.
|
|
116
|
-
|
|
117
|
-
```tsx
|
|
118
|
-
import { useFeatureFlagPayload } from '@posthog/react'
|
|
119
|
-
|
|
120
|
-
function MyComponent() {
|
|
121
|
-
const payload = useFeatureFlagPayload('feature-with-payload')
|
|
122
|
-
|
|
123
|
-
return <div>Config: {JSON.stringify(payload)}</div>
|
|
124
|
-
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
#### useActiveFeatureFlags
|
|
128
|
-
|
|
129
|
-
Get all active feature flags for the current user.
|
|
130
|
-
|
|
131
|
-
```tsx
|
|
132
|
-
import { useActiveFeatureFlags } from '@posthog/react'
|
|
133
|
-
|
|
134
|
-
function MyComponent() {
|
|
135
|
-
const activeFlags = useActiveFeatureFlags()
|
|
136
|
-
|
|
137
|
-
return (
|
|
138
|
-
<ul>
|
|
139
|
-
{activeFlags?.map((flag) => (
|
|
140
|
-
<li key={flag}>{flag}</li>
|
|
141
|
-
))}
|
|
142
|
-
</ul>
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Components
|
|
148
|
-
|
|
149
|
-
#### PostHogFeature
|
|
150
|
-
|
|
151
|
-
A component that renders content based on a feature flag's value. Automatically tracks feature views and interactions.
|
|
152
|
-
|
|
153
|
-
```tsx
|
|
154
|
-
import { PostHogFeature } from '@posthog/react'
|
|
155
|
-
|
|
156
|
-
function MyComponent() {
|
|
157
|
-
return (
|
|
158
|
-
<PostHogFeature flag="new-cta" fallback={<OldButton />}>
|
|
159
|
-
<NewButton />
|
|
160
|
-
</PostHogFeature>
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// With variant matching
|
|
165
|
-
function MyComponent() {
|
|
166
|
-
return (
|
|
167
|
-
<PostHogFeature flag="experiment" match="test" fallback={<ControlVersion />}>
|
|
168
|
-
<TestVersion />
|
|
169
|
-
</PostHogFeature>
|
|
170
|
-
)
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// With payload as render function
|
|
174
|
-
function MyComponent() {
|
|
175
|
-
return (
|
|
176
|
-
<PostHogFeature flag="banner-config">
|
|
177
|
-
{(payload) => <Banner title={payload.title} color={payload.color} />}
|
|
178
|
-
</PostHogFeature>
|
|
179
|
-
)
|
|
180
|
-
}
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
#### PostHogErrorBoundary
|
|
184
|
-
|
|
185
|
-
An error boundary that captures React errors and reports them to PostHog.
|
|
186
|
-
|
|
187
|
-
```tsx
|
|
188
|
-
import { PostHogErrorBoundary } from '@posthog/react'
|
|
189
|
-
|
|
190
|
-
function App() {
|
|
191
|
-
return (
|
|
192
|
-
<PostHogProvider apiKey="<YOUR_PROJECT_API_KEY>">
|
|
193
|
-
<PostHogErrorBoundary>
|
|
194
|
-
<YourApp />
|
|
195
|
-
</PostHogErrorBoundary>
|
|
196
|
-
</PostHogProvider>
|
|
197
|
-
)
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
Please see the main [PostHog docs](https://www.posthog.com/docs).
|
|
9
|
+
- [React library docs](https://posthog.com/docs/libraries/react)
|
|
202
10
|
|
|
203
11
|
## Questions?
|
|
204
12
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/react",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.2",
|
|
4
|
+
"bugs": {
|
|
5
|
+
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
|
+
},
|
|
4
7
|
"description": "Provides components and hooks for React integrations of PostHog.",
|
|
5
8
|
"repository": {
|
|
6
9
|
"type": "git",
|
|
@@ -51,7 +54,7 @@
|
|
|
51
54
|
"tslib": "^2.5.0",
|
|
52
55
|
"typescript": "5.8.2",
|
|
53
56
|
"@posthog-tooling/rollup-utils": "1.1.1",
|
|
54
|
-
"posthog-js": "1.
|
|
57
|
+
"posthog-js": "^1.386.7"
|
|
55
58
|
},
|
|
56
59
|
"scripts": {
|
|
57
60
|
"clean": "rimraf dist",
|