In short
To add a chatbot in React JS, copy the small <Chatbot /> component below into your app: it appends the Nexoply script once from a useEffect and renders nothing itself. Or paste the one-line script into index.html, just before </body>. Either way the chat button appears on every route, with no npm package to install and no AI key to manage: the assistant answers from the business information you add in the dashboard.
- Copy-paste <Chatbot /> component: a useEffect that appends the script once. TypeScript as-is, safe with StrictMode, no cleanup needed.
- No component needed? One script tag in index.html before </body> (Vite: index.html at the project root; Create React App: public/index.html).
- It mounts on document.body inside a Shadow DOM, outside your React root, so re-renders, route changes and your CSS never affect it.
- Open the chat from your own button with window.dispatchEvent(new Event("nexoply:open-chat")).
- Testing on localhost? Add your dev host with its port to Allowed websites, or leave that list empty.
Before you start
You need two things before touching your code:
- A Nexoply account and your chatbot key. Sign up free (the Free plan includes 50 conversations a month, no card needed), then open Chatbot in the dashboard. The install code there already contains your key. It's public, not a secret, and gives no access to the account, so putting it in client-side code is fine.
- The business information the assistant answers from: services and prices, FAQs, policies, opening hours and service areas. The quickest way is to enter the website address and let Nexoply import the public pages, including pages built with JavaScript, so a React site works too. When you sign up with your website, it adds services, FAQs, opening hours, policies and pages by itself, never overwriting what you typed; a scan you start later from the dashboard is shown for review first.
Option 1: A ready-to-copy React chatbot component
Create src/components/Chatbot.tsx. The component renders nothing itself: it adds the Nexoply script to the page once, and the widget draws the chat button and window.
// src/components/Chatbot.tsx
import { useEffect } from "react";
type ChatbotProps = { chatbotKey: string };
export function Chatbot({ chatbotKey }: ChatbotProps) {
useEffect(() => {
// No key, or the script is already on the page: nothing to do.
if (!chatbotKey || document.querySelector("script[data-chatbot]")) return;
const s = document.createElement("script");
s.src = "https://nexoply.com/widget.js";
s.async = true;
s.dataset.chatbot = chatbotKey;
document.body.appendChild(s);
}, [chatbotKey]);
return null;
}Render it once, in the component that stays mounted for the whole visit (usually App). In Vite, put VITE_NEXOPLY_KEY=YOUR-KEY in a .env file:
// src/App.tsx
import { Chatbot } from "./components/Chatbot";
export default function App() {
return (
<>
{/* your layout and routes */}
<Chatbot chatbotKey={import.meta.env.VITE_NEXOPLY_KEY} />
</>
);
}The component works as-is in a TypeScript project with Vite or Create React App. For plain JavaScript, name the file Chatbot.jsx and remove the ChatbotProps type. Create React App doesn't have import.meta.env: pass process.env.REACT_APP_NEXOPLY_KEY instead. Because it's an ordinary component, you can also render it conditionally, for example {hasConsent && <Chatbot chatbotKey={key} />}, to load the chat only after a visitor accepts your cookie banner.
- Why there's no cleanup function. The widget has no API to remove it once loaded, and it lives outside your React tree, so unmounting the component doesn't need to undo anything.
- Why StrictMode is fine. In development, React StrictMode runs effects twice. The querySelector check skips the second run, and even if the script were added twice, the widget mounts only once per chatbot key. Hot reload is safe for the same reason.
- Keep the attribute. The widget reads its settings from its own script tag, so the tag must keep the data-chatbot attribute and a src containing widget.js. Setting s.dataset.chatbot creates that attribute.
Option 2: One script tag in index.html
If you don't need to control loading from code, skip the component. Every React SPA has an HTML shell: in Vite it's index.html at the project root; in Create React App it's public/index.html. Paste the code just before the closing </body> tag:
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<!-- Nexoply AI chatbot -->
<script src="https://nexoply.com/widget.js" data-chatbot="YOUR-KEY" async></script>
</body>Replace YOUR-KEY with the key from the Chatbot page. The script is loaded asynchronously, so it never blocks your app from rendering. Add data-open="true" to the tag if you want the chat window to open on load. That's the whole install: rebuild, deploy, and the button shows on every route.
React chatbot library vs a hosted AI chatbot
Search for "React chatbot" and you'll find two kinds of tools. They solve different problems:
- A chatbot UI library gives you React components for the chat itself: a message list, an input, bubbles, sometimes scripted conversation steps. Everything behind the UI is yours to build: fixed scripts, or a backend that calls an AI model with your own API key, plus the content it answers from, conversation storage and protection against abuse. It fits when chat is part of your product, for example an assistant that works with your users' own data.
- A hosted AI chatbot such as Nexoply ships the UI, the AI and the backend together. It answers visitors' questions only from the business information you add or import (services and prices, FAQs, policies, opening hours) and says so when it doesn't know. There's no AI key and no server code; conversations, unanswered questions and spam protection are handled in the dashboard. It fits a customer-facing website that needs to answer questions about the business.
Want full control over the interface and the model? Start with a library. If you just need visitors' questions answered and don't want to build or run a backend, the hosted widget is less work. See how Nexoply answers from your business information.
How it behaves in a single-page app
- It stays out of your React tree. The widget mounts on document.body, outside your #root element, so re-renders and route changes never remove or restyle it.
- Shadow DOM both ways. Your CSS (Tailwind, CSS modules, global resets) doesn't leak into the chat, and its CSS doesn't leak into your app.
- Route changes just work. With React Router or any history-based router, visitors keep their conversation while they navigate, and page rules (such as a side tab only on /pricing) are re-checked when the URL changes. No extra code.
- No cookies. It stores an anonymous visitor id in localStorage and the current conversation in sessionStorage.
Open the chat from your own button
Want a "Chat with us" button in your hero section or pricing page? Dispatch the nexoply:open-chat event on window:
export function ChatButton() {
return (
<button onClick={() => window.dispatchEvent(new Event("nexoply:open-chat"))}>
Chat with us
</button>
);
}It needs no import: the widget listens for this event on window. If you'd rather show only your own button, set the message button to "No button" with a page rule in the dashboard.
Local testing and troubleshooting
Before you deploy, check these three settings:
- Allowed websites. If you filled in Only allow on these websites (dashboard → Chatbot), add your dev host with its port, such as localhost:5173 for Vite or localhost:3000 for Create React App, or leave the list empty while testing.
- Content-Security-Policy. If your app sends a CSP header or meta tag, allow https://nexoply.com in script-src, connect-src and img-src.
- Check it end to end. Run the app, click the chat button, ask a real question and look for it under Conversations in the dashboard.
If the button still doesn't appear:
- Empty key. The component loads nothing without a key. Log import.meta.env.VITE_NEXOPLY_KEY: Vite only exposes variables that start with VITE_, and you need to restart the dev server after editing .env.
- Blocked request. Look at the browser console and Network tab for CSP errors or a blocked widget.js.
- Chatbot switched off. Check that the Chatbot enabled switch is on.
- Ad blockers. Rarely, a browser extension blocks chat widgets. Try a private window without extensions.
Next steps: customize it without redeploying
Once the script is in, you won't need to touch the code again: changes made in the dashboard reach the site within about a minute. Under Chatbot → Customize you can style the message button (icon or your own image or SVG, text label, shape, size, colours, position) or use a side tab on the edge of the screen instead. Set a different look on phones, and use page rules to change it on chosen routes (for example /pricing, or /services/*). The chat window takes your fonts, colours and logo, and the start screen can offer buttons like Book, Call or WhatsApp.
Using another framework? See how to add a chatbot to Next.js with next/script or the Vue and Nuxt chatbot guide; for a WordPress marketing site, see the WordPress guide. For the full picture, read how the assistant learns your business or compare plans and conversation limits.
Frequently asked questions
Is there a simple React chatbot component?
Yes: the <Chatbot /> component in this guide, under 20 lines with a useEffect, which you copy into your own project. There's no official npm package or component library to install, and you don't need one. The widget is always the script https://nexoply.com/widget.js with a data-chatbot attribute, and all settings live in the dashboard.
Does it work with React Router and other SPA navigation?
Yes. The widget keeps the conversation while visitors navigate client-side and re-checks page rules when the URL changes, with no extra code. It lives outside your React tree, so route changes never remove it.
Do I need a backend or an AI API key?
No. Nexoply runs the AI and the backend, and no AI keys or private data are ever sent to the browser. The only value in your code is the chatbot key, which is public and gives no access to your account.
Will it slow down my app?
Not noticeably. The script loads asynchronously and never blocks rendering, and it runs inside its own Shadow DOM without touching your components.
Does it work with Next.js?
Yes, with the next/script component in your root layout. See the Next.js guide for the App Router and Pages Router snippets.
How do I hide it on some routes?
There's no API to remove it once loaded. Instead, add a page rule with "No button" (dashboard → Chatbot → Customize → Message button → Different on some pages), or turn the chatbot off.



