React Chat - Data Binding
You can bind data to Chat in two ways: with the items array or the dataSource entity.
items
The items array contains all messages in the Chat. To display an initial message, add it to this array. Add subsequent messages to the array to render them in Chat.
- import React, { useCallback, useState } from "react";
- import Chat from "devextreme-react/chat";
- const initialMessage = {
- timestamp: Date.now(),
- author: secondUser,
- text: "Hello! I'm here to help you. How can I assist you today?",
- };
- const App = () => {
- const [messages, setMessages] = useState(initialMessage);
- const onMessageEntered = useCallback(({ message }) => {
- setMessages((prevMessages) => [...prevMessages, message]);
- }, []);
- return (
- <Chat
- onMessageEntered={onMessageEntered}
- items={messages}
- />
- );
- };
dataSource
For a more flexible solution than an array, use the dataSource property. For instance, dataSource allows Chat to handle server-side data processing through Web API, a store, or a DataSource object.
When you pass a store to dataSource, the DataSource instance is created automatically inside the Chat.
When you use dataSource, the reloadOnChange property is crucial:
- When you send a message in a Chat (press the "Send" button), the Chat triggers the store's insert method and adds the message to the store.
- If reloadOnChange is enabled (default), the dataSource reloads: clears all items and calls the load method to update itself. Chat automatically listens to dataSource changes, and updates the message feed with new messages.
- Disable reloadOnChange to manage large numbers of messages, prevent additional load requests, and control message rendering timing.
The following code snippet includes:
- A CustomStore.
- A DataSource (pagination disabled).
- Push API for inserting and updating messages.
reloadOnChange: false.
- import { Chat } from "devextreme-react";
- import { ChatTypes } from "devextreme-react/chat";
- import "devextreme/dist/css/dx.light.css";
- import { useCallback } from "react";
- import DataSource from "devextreme/data/data_source";
- import CustomStore from "devextreme/data/custom_store";
- export default function App() {
- const messages: ChatTypes.Message[] = [
- {
- timestamp: new Date(),
- text: "Hello! I'm here to help you. How can I assist you today?",
- },
- ];
- const customStore = new CustomStore({
- key: "id",
- load: (): Promise<ChatTypes.Message[]> =>
- new Promise((resolve) => {
- setTimeout(() => {
- resolve([...messages]);
- }, 0);
- }),
- insert: (message: ChatTypes.Message): Promise<ChatTypes.Message> =>
- new Promise((resolve) => {
- setTimeout(() => {
- messages.push(message);
- resolve(message);
- });
- }),
- });
- const dataSource = new DataSource({
- store: customStore,
- paginate: false,
- });
- let editing = null;
- let uniqueIndex = messages.length + 1;
- const onMessageEntered = useCallback((e: ChatTypes.MessageEnteredEvent) => {
- if (editing) {
- dataSource.store().push([
- { type: "update", key: editing, data: { text: e.message!.text } },
- ]);
- editing = null;
- } else {
- dataSource.store().push([{ type: "insert", data: { id: uniqueIndex++, ...e.message } }]);
- }
- }, []);
- return (
- <Chat
- dataSource={dataSource}
- reloadOnChange={false}
- onMessageEntered={onMessageEntered}
- />
- );
- }