This guide shows you how to add Heyo to your React application. Works with Create React App, Vite, and any React setup.
Install the SDK for full TypeScript support and programmatic control:
npm install @heyo.so/js
Then initialize it in your App component:
import { useEffect } from 'react';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
function App() {
useEffect(() => {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}, []);
return <div className="App">{/* Your app content */}</div>;
}
export default App;
Load the script tag dynamically in your App component:
import { useEffect } from 'react';
function App() {
useEffect(() => {
const script = document.createElement('script');
script.src = 'https://heyo.so/embed/script';
script.defer = true;
script.setAttribute('data-project-id', 'YOUR_PROJECT_ID');
document.head.appendChild(script);
return () => {
document.head.removeChild(script);
};
}, []);
return <div className="App">{/* Your app content */}</div>;
}
export default App;
The Heyo SDK includes TypeScript definitions. For the script method, you can add type definitions:
declare global {
interface Window {
HEYO: {
init: (config: { projectId: string }) => void;
show: () => void;
hide: () => void;
toggle: () => void;
};
}
}
Create a custom hook to control the chat widget:
import { useEffect } from 'react';
const useChatWidget = () => {
useEffect(() => {
// Widget will be available after initialization
return () => {
window.HEYO?.hide();
};
}, []);
const showChat = () => window.HEYO?.show();
const hideChat = () => window.HEYO?.hide();
const toggleChat = () => window.HEYO?.toggle();
return { showChat, hideChat, toggleChat };
};
// Usage in component
function MyComponent() {
const { showChat } = useChatWidget();
return <button onClick={showChat}>Open Chat</button>;
}
Pass user context to provide better support:
useEffect(() => {
HEYO.init({
projectId: 'YOUR_PROJECT_ID',
user: {
name: 'John Doe',
email: '[email protected]',
},
});
}, []);
defer attribute to avoid blocking page renderingMake sure you've added your correct Project ID and the script is loading after the DOM is ready.
If you're seeing multiple widgets, ensure you're only initializing Heyo once in your app.
In development, React StrictMode may cause useEffect to run twice. This is normal and won't affect production.