Chatbot for React Website Projects: Load the Script Once, Survive Route Changes and Avoid Double Installs (Copy-Ready Snippets)

Chatbot for React website builds has one trap that plain HTML sites do not: React changes the page without reloading, so a chat widget can load twice, or never notice that the visitor moved. This guide shows how to load the script once, tell it about route changes, and pass a logged-in user, with snippets you can copy.

1script, loaded once
0npm packages needed
₹0free plan, 2 websites
100free visitors per site each month

⚡ In short

  • LiveChatBot has no React npm package. You load the same embed script as on any site.
  • The simplest place is your index.html. If you load it from a component, guard against a second copy.
  • React Router does not reload the page, so call pageView after each route change.
  • Pass a logged-in user with setVisitor, only after a real login.
Chatbot for react website in LiveChatBot: AI Agent knowledge sources and a sample chat answered from them
How it looks in LiveChatBot: AI Agent knowledge sources and a sample chat answered from them.

The approach

How a chatbot for React website apps differs from plain pages

A React app is usually one HTML file and a lot of JavaScript. The browser loads the file once, then React swaps what you see as the visitor clicks around. For a chat widget that matters in two ways.

It loads once per tab

The widget attaches itself to the page the first time the script runs. You want exactly one script tag for the life of the tab, not one per component render.

It can miss navigation

A widget that only watches full page loads will think the visitor stayed on the landing page. Page-based Flows and visitor tracking then see the wrong page. The JavaScript API method pageView fixes this.

There is no React component to import and no wrapper library. You work with the script and the global window.LiveChatBot object, which offers open, close, show, hide, setVisitor, sendMessage, pageView and on.

Option one

Option one: put the script in index.html

For most apps this is the right answer. Open the HTML file your build starts from, which is public/index.html in older Create React App projects and the root index.html in Vite, and paste the dashboard’s script just before the closing body tag.

Why it is the safest

It runs once, outside React’s render cycle, so Strict Mode’s repeated effects, hot reloads and re-renders cannot add a second copy. There is nothing to clean up either.

What to watch

Use the key from your dashboard exactly as given. If you want a different key for staging, many build tools can substitute an environment variable into the HTML at build time; check your tool’s documentation for that.

Try it

Copy-ready snippets for a React app

Pick a tab, copy the line, and replace YOUR_KEY with your website key from the dashboard. The lines are generic, so adjust names to match your project.

<script src=”https://livechatbot.in/widget.js” data-key=”YOUR_KEY” async></script>
useEffect(() => {
if (document.getElementById(‘lcb-widget’)) { return; }
const s = document.createElement(‘script’);
s.id = ‘lcb-widget’;
s.src = ‘https://livechatbot.in/widget.js’;
s.async = true;
s.setAttribute(‘data-key’, ‘YOUR_KEY’);
document.body.appendChild(s);
}, []);
const location = useLocation();
useEffect(() => {
if (window.LiveChatBot) { window.LiveChatBot.pageView(); }
}, [location.pathname]);
useEffect(() => {
if (user) { if (window.LiveChatBot) { window.LiveChatBot.setVisitor({ name: user.name, email: user.email }); } }
}, [user]);

Option two

Option two: load it from a component

Sometimes you cannot edit the HTML file, for example when a framework wraps it, or you want chat only inside part of the app. Then load the script from a component that mounts once, such as your top-level App.

The snippet in the second tab checks for an element with a fixed id before it adds anything. That check is what stops duplicates when React Strict Mode runs effects twice in development, or when the component remounts.

Remove it?

You usually should not remove the script when the component unmounts. A widget that appears and disappears is confusing. If the chat should show only for some routes, use the dashboard’s where-it-shows settings, or call hide and show from the JavaScript API.

Navigation

Telling a chatbot for React website routes that the page changed

With React Router, a small effect in a component that sits inside the router is enough. It watches the pathname and calls pageView whenever it changes.

Two details

First, the check on window.LiveChatBot means the call is skipped if the widget has not finished loading. On the very first render this avoids reporting the landing page twice. If you see the first page counted twice anyway, skip the call on the first run with a ref.

Second, this only matters if you use page-based Flows or care about visitor tracking accuracy. A site with one page of content can skip it. Our API snippets guide shows the same idea for other frameworks.

Logged-in users

Pass the signed-in customer to your team

If your app has accounts, call setVisitor with the user’s name and an email or phone number after they sign in. Your team then sees who is chatting, and you stop asking for an email in every conversation.

Cautions

  • Only call it after a real login, never before the user gives details.
  • Mention website chat in your privacy notice.
  • The widget may load a moment after your effect runs. Test the sign-in flow on a slow connection and confirm the visitor really appears in the inbox.

Testing

Test a chatbot for React website builds in development and production

In development

Run the dev server, open the page and check the browser’s elements panel. Search for the widget address. You should find one script, even after a hot reload. If there are two, your loader lacks the guard or sits in two components.

In production

Build, deploy, and open the live site in a private window. Click through three routes, then open the Flows or visitor tracking views in the dashboard and check the page names match what you clicked.

Common problems

  • No bubble: an ad blocker, a content security policy or a wrong key. The console says which.
  • Bubble covers your UI: move the widget position in the dashboard.
  • Chat on a login page: hide it with the where-it-shows settings, which is easier for the team to maintain than code.

Our troubleshooting guide covers the missing-widget checks in more detail.

Honest gaps

What to know before you build around it

  • There is no React component, hook or npm package. Everything runs through the script and window.LiveChatBot.
  • The bot cannot read your React state or API. It answers from the knowledge you write for the AI Agent, and from saved replies.
  • No pre-chat form, no badge removal, no WhatsApp inbox, no iOS team app.
  • If you use server-side rendering, the script must still run in the browser, not on the server.

Free visitors are 100 per website each month, and pricing lists the one-time packs.

Frequently asked questions

Is there a React package for LiveChatBot?

No. You add the same embed script and use window.LiveChatBot for open, close, show, hide, setVisitor, sendMessage, pageView and on.

Where should I put the script in a React app?

In the HTML file your build starts from, just before the closing body tag. Load it from a component only if you cannot edit that file.

Why does the chat show the wrong page?

React changes pages without a reload. Call pageView after each route change so tracking and page rules follow the visitor.

Does a chatbot for React website apps work with Vite and Create React App?

Yes. Both start from an HTML file where you can paste the script, or you can load it from a component.

What does it cost?

The free plan covers two websites and 100 new visitors per website each month. Packs are one-time and never expire.

Keep reading

Related guides

📘

Chatbot for Custom Website Builds: Where the Script Goes in Your Templates, Which Environment Gets Which Key, and What Your Security Rules Must Allow

Read the guide →

🧭

Chatbot for Next.js Website Projects: Add the Script in Your Root Layout, Handle Client-Side Navigation and Keep the Key in an Environment Variable (Timeline)

Read the guide →

🛠️

Chatbot for HTML Website Rollout: Get One Snippet onto Every Page and Prove It Works on All of Them

Read the guide →

Add chat to your React app

One script, a free plan for two websites and no per-seat charges. Start on livechatbot.in.