Hooks
React hooks for programmatic access to UpsellSDK.
useUpsell
Access the SDK context for tracking and identifying:
import { useUpsell } from '@upsellsdk/react'
function MyComponent() {
const { track, identify, user } = useUpsell()
const handleExport = () => {
track('export_attempted', { format: 'csv' })
// ... do export
}
return (
<button onClick={handleExport}>
Export as CSV
</button>
)
}
Return Value
| Property | Type | Description |
|---|---|---|
track | function | Track an event |
identify | function | Identify a user |
user | object | Current user info |
openUpgradeModal | function | Open the upgrade modal |
useFeature
Check feature access and get an upgrade function:
import { useFeature } from '@upsellsdk/react'
function AnalyticsButton() {
const { allowed, requestAccess } = useFeature('analytics')
const handleClick = () => {
if (allowed) {
navigate('/analytics')
} else {
requestAccess() // Shows upgrade modal
}
}
return (
<button onClick={handleClick}>
View Analytics
{!allowed && <span className="badge">Pro</span>}
</button>
)
}
Parameters
| Parameter | Type | Description |
|---|---|---|
featureKey | string | The feature to check |
Return Value
| Property | Type | Description |
|---|---|---|
allowed | boolean | Whether user has access |
requestAccess | function | Opens upgrade modal |