Getting Started with the React AI AssistView Component
28 Aug 202510 minutes to read
This section explains how to create a simple AI AssistView component and configure its available functionalities in React.
To get started quickly with the React AI AssistView component, you can check out this video tutorial:
Dependencies
The following list of dependencies is required to use the React AI AssistView
component in your application. The component is distributed as part of the @syncfusion/ej2-react-interactive-chat
package.
|-- @syncfusion/ej2-react-interactive-chat
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-inputs
Setup for Local Development
The recommended way to create a new React application is by using create-react-app.This tool sets up a development environment with everything you need to build, test, and optimize your application for production. Refer to the installation instructions of create-react-app
.
npx create-react-app my-app
cd my-app
npm start
or
yarn create react-app my-app
cd my-app
yarn start
To create a React application with TypeScript
environment, run the following command.
npx create-react-app my-app --template typescript
cd my-app
npm start
Besides using the npx package runner tool, also create an application from the npm init
. To begin with the npm init
, upgrade the npm
version to npm 6+
.
npm init react-app my-app
cd my-app
npm start
Adding Syncfusion® Packages
All Syncfusion Essential® JS 2 packages are available on the npmjs.com
public registry. You can choose the component that you want to install.
To install the AI AssistView component package, run the following command:
npm install @syncfusion/ej2-react-interactive-chat --save
Adding the AI AssistView Component
Now, you can start adding AI AssistView component in the application. For getting started, add the AI AssistView component by using <AIAssistViewComponent>
tag directive in src/App.tsx
file using following code. Now place the below AI AssistView code in the src/App.tsx
.
[Functional-component]
import { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
return (
// specifies the tag for render the AI AssistView component
<AIAssistViewComponent id="aiAssistView"></AIAssistViewComponent>
);
}
ReactDOM.render(<App />, document.getElementById('aiAssistView'));
Adding CSS References
To apply styling to the AI AssistView component, import the required CSS theme files into your src/App.css
file. The following example uses the Material theme, but other themes like Bootstrap 5, Tailwind CSS, or Fluent are also available.
/* import the AI AssistView dependency styles */
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-notifications/styles/material.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-interactive-chat/styles/material.css";
Run the Application
With the configuration complete, run the application to see the AI AssistView component rendered in your browser.
npm start
[Functional-componnet]
import { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
return (
// specifies the tag for render the AI AssistView component
<AIAssistViewComponent id="aiAssistView"></AIAssistViewComponent>
);
}
ReactDOM.render(<App />, document.getElementById('container'));
import { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
return (
// specifies the tag for render the AI AssistView component
<AIAssistViewComponent id="aiAssistView"></AIAssistViewComponent>
);
}
ReactDOM.render(<App />, document.getElementById('container'));
Configure Suggestions and Asynchronous Responses
The AI AssistView component can be configured to guide user interactions and provide dynamic content from an AI backend.
- promptSuggestions: Use this property to display a list of predefined suggestion chips that users can click to submit a prompt.
- promptRequest: This event is triggered when a user sends a prompt. You can use its event handler to make an asynchronous API call to an AI service and stream the response back to the component.
The following example demonstrates how to define prompt suggestions and handle the promptRequest
event to simulate fetching a response from a backend.
import { AIAssistViewComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef(null);
const promptSuggestions = [
"How do I prioritize my tasks?",
"How can I improve my time management skills?"
];
const prompts = [
{
prompt: "How do I prioritize my tasks?",
response: "Prioritize tasks by urgency and impact: tackle high-impact tasks first, delegate when possible, and break large tasks into smaller steps. For more assistance, feel free to ask—I’m here to help!"
},
{
prompt: "How can I improve my time management skills?",
response: "To improve time management skills, try setting clear goals, using a planner or digital tools, prioritizing tasks, breaking tasks into smaller steps, and minimizing distractions. Regularly review and adjust your approach for better efficiency."
}
];
const onPromptRequest = (args) => {
setTimeout(() => {
let foundPrompt = prompts.find((promptObj) => promptObj.prompt === args.prompt);
let defaultResponse = 'For real-time prompt processing, connect the AI AssistView component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
assistInstance.current.addPromptResponse(foundPrompt ? foundPrompt.response : defaultResponse);
}, 1000);
};
return (
// specifies the tag for render the AI AssistView component
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest} promptSuggestions={promptSuggestions}></AIAssistViewComponent>
);
}
ReactDOM.render(<App />, document.getElementById('container'));
import { AIAssistViewComponent,PromptRequestEventArgs, PromptModel } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
const assistInstance = React.useRef<AIAssistViewComponent>(null);
const promptSuggestions: string[] = [
"How do I prioritize my tasks?",
"How can I improve my time management skills?"
];
const prompts: PromptModel[] = [
{
prompt: "How do I prioritize my tasks?",
response: "Prioritize tasks by urgency and impact: tackle high-impact tasks first, delegate when possible, and break large tasks into smaller steps. For more assistance, feel free to ask—I’m here to help!"
},
{
prompt: "How can I improve my time management skills?",
response: "To improve time management skills, try setting clear goals, using a planner or digital tools, prioritizing tasks, breaking tasks into smaller steps, and minimizing distractions. Regularly review and adjust your approach for better efficiency."
}
];
const onPromptRequest = (args: PromptRequestEventArgs) => {
setTimeout(() => {
let foundPrompt = prompts.find((promptObj) => promptObj.prompt === args.prompt);
let defaultResponse = 'For real-time prompt processing, connect the AI AssistView component to your preferred AI service, such as OpenAI or Azure Cognitive Services. Ensure you obtain the necessary API credentials to authenticate and enable seamless integration.';
assistInstance.current.addPromptResponse(foundPrompt ? foundPrompt.response : defaultResponse);
}, 1000);
};
return (
// specifies the tag for render the AI AssistView component
<AIAssistViewComponent id="aiAssistView" ref={assistInstance} promptRequest={onPromptRequest} promptSuggestions={promptSuggestions}></AIAssistViewComponent>
);
}
ReactDOM.render(<App />, document.getElementById('container'));