Trình bao React
Một provider và hai hook đặt trên SDK TypeScript. Nó cài một client cho mỗi trang, nên chế độ nghiêm ngặt của React không nhân đôi sự kiện của bạn.
Nơi phát hành
@prodantix/react on npm. It wraps @prodantix/sdk, so install both.
Cài đặt
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 |
Provider
Hãy gắn ProdantixProvider một lần, gần gốc. Nó tạo client web ở lần gắn đầu tiên rồi dùng lại sau đó, vì tạo cái thứ hai sẽ vá History API hai lần và nhân đôi mọi sự kiện được thu tự động.
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>
);Các hook
useProdantix trả về client của trang, hoặc null khi ở phía máy chủ, trước khi effect chạy, và bên ngoài provider. useMessenger trả về handle để mở và đóng tiện ích, và vẫn là null cho tới khi cấu hình dự án được đọc và cho biết đã bật.
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>
);
};Khi nó không gửi gì
Một ứng dụng vẫn phát hành y như vậy khi tắt Prodantix, nên thiếu khoá hay host không phải là lỗi. prodantixConfig cho biết cả hai giá trị có mặt hay không, còn skipReason nói trong một dòng vì sao lần gắn này sẽ không gửi gì, thay vì lặng lẽ trả về.
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));
}