React SDK
The React SDK is a collection of components and hooks that can be used to build React applications that interact with the ComPilot Widget
Installation
The React SDK can be installed with yarn
, npm
, or pnpm
.
- yarn
- npm
- pnpm
yarn add @compilot/react-sdk
npm install --save @compilot/react-sdk
pnpm add @compilot/react-sdk
Usage
Create a configuration object
- Regular mode - see step 4 in Regular Mode: Step by step with React or Next.js
- Web3 mode - see step 4 in Web3 Mode: Step by step with React or Next.js
Add the ComPilotProvider
First add the ComPilotProvider
at the root of your application.
import { ComPilotProvider } from "@compilot/react-sdk";
import { config } from "./config";
function App() {
return <ComPilotProvider>{/* Your application */}</ComPilotProvider>;
}
Then you can use any of the hooks provided by the SDK to interact with the ComPilot Widget.
import {
useCustomerStatus,
useIsAuthenticated,
useIsLoading,
useOpenWidget,
} from "@compilot/react-sdk";
function MyComponent() {
const authenticate = useAuthenticate();
const isAuthenticated = authenticate.data === true;
const isLoading = authenticate.isPending;
const openWidget = useOpenWidget();
const customerStatus = useCustomerStatus();
return (
<div>
<p>Is authenticated: {isAuthenticated ? "Yes" : "No"}</p>
<p>Is loading: {isLoading ? "Yes" : "No"}</p>
<p>Customer status: {customerStatus}</p>
<button onClick={openWidget}>Open widget</button>
</div>
);
}