Stream Chat React Components
Stream Chat React is the official React component library for building real-time chat UIs using the Stream Chat API. The current stable version is 14.0.1, with frequent patch releases and significant major version updates, such as the recent v14.0.0 which introduced a complete visual and architectural refresh. This SDK provides a comprehensive set of UI components for various chat use cases, including livestream, team collaboration, messaging, and customer support, abstracting away much of the complexity of real-time communication. Key differentiators include its deep integration with the Stream Chat backend service, a highly customizable component system, and a robust API for handling chat functionality, making it a powerful tool for developers needing to implement rich chat experiences quickly.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'connectUser') OR client.connectUser is not a function
cause The `StreamChat` client instance from `stream-chat` has not been correctly initialized or passed to the `Chat` component.fixEnsure `StreamChat.getInstance(API_KEY)` is called and the resulting client object is passed as the `client` prop to the top-level `<Chat>` component. -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Incorrect import of `stream-chat-react` components, typically mixing default and named imports or using CommonJS `require()` in an ESM context.fixVerify that all `stream-chat-react` components are imported using named imports (e.g., `import { Chat, Channel } from 'stream-chat-react';`). -
Failed to compile. Module not found: Error: Can't resolve 'stream-chat-react/dist/css/v2/index.css'
cause This error occurs after upgrading to `stream-chat-react` v14.0.0-beta.4 or higher, where the CSS import path was changed.fixUpdate the CSS import statement to `import 'stream-chat-react/dist/css/index.css';`. -
React Hook "useState" cannot be called inside a callback. React Hooks must be called in a React function component or at the top level of a custom React Hook.
cause Using React Hooks (like `useState`, `useEffect`) within class components or outside of a functional component's top level, often seen during migration or incorrect component structure.fixEnsure all React Hooks are called within functional components or custom hooks and follow the Rules of Hooks.
Warnings
- breaking Version 14.0.0 represents a complete visual and architectural overhaul. It includes a redesigned component system, new message composer, overhauled theming, and significant breaking changes to component APIs, CSS class names, and context wrappers. Refer to the official migration guide.
- breaking The CSS import path changed in v14.0.0-beta.4. Previously, styles were imported from `stream-chat-react/dist/css/v2/*`. This path no longer works.
- breaking Several components were removed in v14.0.0-beta.2, including `ActionsIcon`, `ReactionIcon`, `ThreadIcon`, and `MessageErrorIcon`. These icons are now handled internally or replaced by new component structures.
- breaking Breaking changes in `v14.0.0-beta.7` include the removal of `onlySenderCanEdit` from `MessageProps` and the deprecation/removal of several legacy notification text callback props from `Message` (e.g., `getDeleteMessageErrorNotification`).
- gotcha The `stream-chat-react` library has peer dependencies on specific React versions (`^19.0.0 || ^18.0.0 || ^17.0.0`). Using an incompatible React version can lead to build errors or runtime issues.
- gotcha The core `StreamChat` client must be imported from the `stream-chat` package, not `stream-chat-react`. These are distinct libraries, with `stream-chat-react` building UI on top of `stream-chat`'s client logic.
Install
-
npm install stream-chat-react -
yarn add stream-chat-react -
pnpm add stream-chat-react
Imports
- Chat
const Chat = require('stream-chat-react').Chat;import { Chat } from 'stream-chat-react'; - Channel
import Channel from 'stream-chat-react/Channel';
import { Channel } from 'stream-chat-react'; - MessageList
import { MessageList } from 'stream-chat-react'; - MessageInput
import { MessageInput } from 'stream-chat-react'; - StreamChat (client)
import { StreamChat } from 'stream-chat-react';import { StreamChat } from 'stream-chat'; - CSS Styles
import 'stream-chat-react/dist/css/v2/index.css';
import 'stream-chat-react/dist/css/index.css';
Quickstart
import React, { useEffect, useState } from 'react';
import { StreamChat } from 'stream-chat';
import { Chat, Channel, ChannelHeader, MessageList, MessageInput, Window, LoadingIndicator } from 'stream-chat-react';
import 'stream-chat-react/dist/css/index.css';
const API_KEY = process.env.STREAM_API_KEY ?? '';
const USER_ID = process.env.STREAM_USER_ID ?? 'john-doe';
const USER_TOKEN = process.env.STREAM_USER_TOKEN ?? 'YOUR_USER_TOKEN'; // Generate this on your backend
const client = StreamChat.getInstance(API_KEY);
function App() {
const [channel, setChannel] = useState(null);
useEffect(() => {
async function initChat() {
if (!API_KEY || !USER_ID || !USER_TOKEN) {
console.error('Please provide STREAM_API_KEY, STREAM_USER_ID, and STREAM_USER_TOKEN environment variables.');
return;
}
try {
await client.connectUser(
{
id: USER_ID,
name: USER_ID,
image: `https://getstream.io/random_png/?id=${USER_ID}&name=${USER_ID}`,
},
USER_TOKEN,
);
const newChannel = client.channel('messaging', 'react-chat-demo', {
name: 'React Chat Tutorial',
members: [USER_ID, 'other-user-id'], // Add other users you want in the channel
});
await newChannel.watch();
setChannel(newChannel);
} catch (error) {
console.error('Failed to connect or join channel:', error);
}
}
initChat();
return () => {
client.disconnectUser();
};
}, []);
if (!channel) return <LoadingIndicator />;
return (
<Chat client={client} theme='messaging light'>
<Channel channel={channel}>
<Window>
<ChannelHeader />
<MessageList />
<MessageInput />
</Window>
</Channel>
</Chat>
);
}
export default App;