React Chatbot Tutorial: Build a Gemini AI Chat Interface

Updated on Sep 21,2025

Table of Contents

In today's digital landscape, chatbots are becoming increasingly essential for businesses seeking to enhance customer engagement and streamline communication. This blog post guides you through the process of creating a modern and interactive chatbot interface using React, enhanced with the power of Google's Gemini AI. We'll cover everything from setting up the basic UI elements to integrating the Gemini API for dynamic and intelligent responses.

Key Points

Building a React-based chatbot interface with a modern, user-friendly design.

Integrating Gemini AI for intelligent and dynamic conversational responses.

Implementing a responsive and visually appealing chat window with customizable features.

Leveraging React hooks for efficient state management and component logic.

Implementing auto-scrolling and displaying of chat history.

Using material symbols and other free icon libraries for an engaging user experience.

Utilizing a chatbot icon as a React component.

Creating chatbot footer with input value submission.

Getting Started: Setting Up Your React Chatbot Project

Why Choose React for Your Chatbot?

React offers a component-based architecture, making it ideal for building complex UIs like a chatbot. Its virtual DOM ensures efficient updates and a smooth user experience. The extensive React ecosystem, combined with tools like Create React App or Vite, simplifies project setup and development. Further, React’s popularity means ample resources and community support are available, making development faster and easier.

Initial Project Setup with Vite

To begin, we'll set up a React project using Vite, a fast and lightweight build tool. First, ensure you have Node.js installed to run React projects.

Here's how to set up the environment:

  1. Open your terminal.
  2. Navigate to your projects directory.
  3. Use the command: npm create vite@latest ./ --template react to generate a new React project.

Once created, open the project in Visual Studio Code (VS Code) or your preferred IDE. Now, delete unnecessary files. In the 'src' folder, remove assets folder, App.css file, and also remove the styles in index.css . This provides a clean slate for our custom chatbot design. Also, add import React from 'react'; to the app.jsx.

Laying the Foundation: Setting up essential components

To create the main structure:

  • Create a 'components' folder in the src directory. Inside this folder, create the ChatForm.jsx and ChatMessage.jsx files.

These actions will create the necessary component files that we can work on.

Creating a Chatbot Icon Component

Let’s start by creating the ChatbotIcon component. Add a new component ChatbotIcon.jsx inside components directory. This section focuses on creating a reusable React component for displaying a chatbot icon. It emphasizes using SVG (Scalable Vector Graphics) for creating vector images that scale without losing quality, ensuring the icon looks sharp on any screen. Google Fonts can also be used for importing vector based Icons

 import React from 'react';

 const ChatbotIcon = () => {
   return (
    <svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 1024 1024">
      <path d="M738.3 287.6H285.7c-59 0-106.8 47.8-106.8 106.8v303.1c0 59 47.8 106.8 106.8 106.8h81.5v111.1c0 .7 1.1 1.4 7.1 16.6c.9 3.8-1.8 4.1-10.6.8H117.4l43.6-4c5.9 0 10.6-.8 10.6-10.6H806.8c59 0 106.8-47.8 106.8-106.8V394.5c0-59-47.8-106.8-106.8-106.8zm86.7 644.5v45.3l-26-9c-3.6-.9-36.9-26.3-36.9-49.7 0-28.6 23.3-51.9 51.9-51.9s51.9 23.3 51.9 51.9c0 23.3-26 26.3-36.9 49.7l-26 9z"/>
   </svg>
   );
 };
 export default ChatbotIcon;

Key Aspects:

  • SVG Integration: The code includes an SVG element that defines the chatbot icon. SVGs are resolution-independent, ensuring the icon looks crisp on different devices and screen sizes.
  • Path Definition: The path element contains the vector data that draws the shape of the chatbot icon. The d attribute specifies a series of drawing commands.
  • Export: The ChatbotIcon component is exported, making it available for use in other parts of the React application.

Building the Essential Chatbot UI in React

Structuring the Chatbot Interface

It’s important to get each part of the chat screen looking perfect. We start off with the header which contains our label and chatbot face. It also contains a closing button which we’ll be toggling with react. We’ll be making use of useRef for auto scrolling, useState for managing responses and other methods that will bring all the components together.

We use a simple function to use a react useState boolean that switches the chatbot window visiblity on and off, to either close it or leave it open.

 const [showChatbot, setShowChatbot] = useState(false);
 const toggleChatbotPopup = () => {
  setShowChatbot(!showChatbot);
 };
 //Code inside the header to close the bot
     <button onClick={toggleChatbotPopup} className='material-symbols-rounded close' id='toggle-button'>keyboard_arrow_down</button>

Custom Styling With index.css

To have an overall purple theme, we’ll be focusing on the main index.css file, which contains:

  • The linear gradient background in the body
  • The chatbot popup display with the opacity turned on, making it visible and adding it to the flow of the document
  • The header info will be a space between with the label and icon
  • Basic styles for the close/down toggle button at the top of the bot

In order to use it, you'll need to update index.html file with Font api.

Also remember to add inter import in order for your styling to work.

 @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
 body {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(#F4F0FF, #DACDFF);
  display: flex;
  align-items: center;
  justify-content: center;
 }

 .container {
  transform: scale(0.2);
 }

 .chatbot-popup {
  position: relative;
  width: 420px;
  overflow: hidden;
  background: #fff;
  border-radius: 15px;
  box-shadow: 0 0 128px 0 rgba(0,0,0,0.1), 0 32px 64px -48px rgba(0,0,0,0.5);
 }

 .chatbot-popup .chat-header {
  display: flex;
  padding: 15px 22px;
  align-items: center;
  justify-content: space-between;
  background: #6D4FC2;
 }

 .chatbot-popup header-info {
  display: flex;
  gap: 10px;
  align-items: center;
 }

 #chatbot-toggler span:first-child {
  opacity: 0;
 }
 .material-symbols-rounded {
  font-variation-settings:
  'FILL' 0,
  'wght' 400,
  'GRAD' 0,
  'opsz' 24;
 }

Frequently Asked Questions

What is the purpose of the ChatForm component's form action attribute set to '#'?
The form action attribute set to '#' is often used as a placeholder, preventing the form from navigating to a new page upon submission. It's a common practice in single-page applications (SPAs) like those built with React, where the client-side JavaScript handles form submission and data processing instead of triggering a full page reload or navigation.

Related Questions

How to dynamically update chatbot prompts?
To dynamically update chatbot prompts, you can manage the prompts as state variables in your React component. When the user interacts with the chatbot, update the state using the useState hook. This triggers a re-render, updating the chatbot's prompts based on the new state. For instance, if the user selects a specific option, you can modify the prompt state to reflect their choice. Remember to handle these changes efficiently to ensure a smooth user experience. [example Javascript code would be included here]

Most people like