React wrapper
Isang provider at dalawang hook sa ibabaw ng TypeScript SDK. Isang client lang ang ini-install nito bawat pahina, kaya hindi dinodoble ng React strict mode ang iyong mga event.
Saan ito nakalathala
@prodantix/react on npm. It wraps @prodantix/sdk, so install both.
I-install
bun add @prodantix/sdk @prodantix/react| Export | What it is |
|---|---|
| ProdantixProvider | Creates the client once per page and provides it |
| useProdantix | The client for the page, or null |
| useMessenger | A handle that opens and closes the widget, or null |
| prodantixConfig | The config when both key and host are present, else null |
| skipReason | Why this mount will send nothing, or null |
| resetProdantixForTest | Clears the once-per-page install, for tests |
Ang provider
I-mount ang ProdantixProvider nang isang beses, malapit sa root. Ginagawa nito ang web client sa unang mount at ginagamit muli pagkatapos, dahil ang paggawa ng pangalawa ay magpa-patch sa History API nang dalawang beses at dodoblehin ang bawat autocaptured na event.
import { ProdantixProvider } from '@prodantix/react';
export const Providers = ({ children }: { children: ReactNode }) => (
<ProdantixProvider
options={{
apiKey: process.env.NEXT_PUBLIC_PRODANTIX_KEY,
host: 'https://eu.api.prodantix.com',
messengerHost: 'https://eu.edge.prodantix.com',
}}
>
{children}
</ProdantixProvider>
);Ang mga hook
Ibinabalik ng useProdantix ang client para sa pahina, o null sa server, bago tumakbo ang effect, at sa labas ng provider. Nagbabalik ang useMessenger ng handle na nagbubukas at nagsasara ng widget, at nananatiling null hangga sa mabasa ang konfigurasyon ng proyekto at sabihin nitong naka-enable.
import { useMessenger, useProdantix } from '@prodantix/react';
export const HelpButton = () => {
const prodantix = useProdantix();
const messenger = useMessenger();
// Both are null until the provider effect has run, so neither is assumed.
return (
<button
onClick={() => {
prodantix?.capture('help.opened');
messenger?.open();
}}
type="button"
>
Get help
</button>
);
};Kapag wala itong ipinapadala
Pareho pa ring naipapadala ang isang app kahit naka-off ang Prodantix, kaya hindi error ang nawawalang key o host. Sinasabi ng prodantixConfig kung nasa lugar ang dalawang halaga, at sinasabi ng skipReason sa isang linya kung bakit walang ipapadala ang isang mount, sa halip na tahimik na bumalik.
import { prodantixConfig, skipReason } from '@prodantix/react';
const env = {
host: process.env.NEXT_PUBLIC_PRODANTIX_HOST,
key: process.env.NEXT_PUBLIC_PRODANTIX_KEY,
};
// null when either value is missing: the app runs with Prodantix switched off.
if (prodantixConfig(env) === null) {
console.warn(skipReason(env, null));
}