react-native-gifted-chat README

The project's README from github.com/FaridSafi/react-native-gifted-chat. As one markdown file: README.md.

npm version npm downloads build platforms TypeScript Expo compatible

React Native Gifted Chat

The most complete chat UI for React Native & Web

Try GiftedChat on Expo Snack

[!NOTE] Active development has moved. This package is now in maintenance mode and isn't receiving new features. Development continues - including streaming AI messages and the latest fixes - in a maintained fork: @kesha-antonov/react-native-chat. New projects are encouraged to use it; migrating is mostly a rename (see the fork's migration guide).


✨ Features

         


Sponsors

Le Reacteur - Coding Bootcamp in Paris co-founded by Farid Safi
Stream - Scalable chat API/Server written in Go (API Tour | Tutorial)
Ethora - A complete app engine featuring GiftedChat (GitHub)

📚 React Key Concepts (2nd ed.)


📖 Table of Contents


📋 Requirements

Requirement Version
React Native >= 0.70.0
iOS >= 13.4
Android API 21+ (Android 5.0)
Expo SDK 50+
TypeScript >= 5.0 (optional)

📦 Installation

Expo Projects

npx expo install react-native-gifted-chat react-native-reanimated react-native-gesture-handler react-native-safe-area-context react-native-keyboard-controller

Bare React Native Projects

Step 1: Install the packages

Using yarn:

yarn add react-native-gifted-chat react-native-reanimated react-native-gesture-handler react-native-safe-area-context react-native-keyboard-controller

Using npm:

npm install --save react-native-gifted-chat react-native-reanimated react-native-gesture-handler react-native-safe-area-context react-native-keyboard-controller

Step 2: Install iOS pods

npx pod-install

Step 3: Configure react-native-reanimated

Follow the react-native-reanimated installation guide to add the Babel plugin.


🚀 Usage

Basic Example

import React, { useState, useCallback, useEffect } from 'react'
import { GiftedChat } from 'react-native-gifted-chat'
import { useHeaderHeight } from '@react-navigation/elements'

export function Example() {
  const [messages, setMessages] = useState([])

  // keyboardVerticalOffset = distance from screen top to GiftedChat container
  // useHeaderHeight() returns status bar + navigation header height
  const headerHeight = useHeaderHeight()

  useEffect(() => {
    setMessages([
      {
        _id: 1,
        text: 'Hello developer',
        createdAt: new Date(),
        user: {
          _id: 2,
          name: 'John Doe',
          avatar: 'https://placeimg.com/140/140/any',
        },
      },
    ])
  }, [])

  const onSend = useCallback((messages = []) => {
    setMessages(previousMessages =>
      GiftedChat.append(previousMessages, messages),
    )
  }, [])

  return (
    <GiftedChat
      messages={messages}
      onSend={messages => onSend(messages)}
      user={{
        _id: 1,
      }}
      keyboardAvoidingViewProps={{ keyboardVerticalOffset: headerHeight }}
    />
  )
}

💡 Tip: Check out more examples in the example directory including Slack-style messages, quick replies, and custom components.


📊 Data Structure

Messages, system messages, and quick replies follow the structure defined in Models.ts.

Message Object Structure
interface IMessage {
  _id: string | number
  text: string
  createdAt: Date | number
  user: User
  image?: string
  video?: string
  audio?: string
  system?: boolean
  sent?: boolean
  received?: boolean
  pending?: boolean
  quickReplies?: QuickReplies
}

interface User {
  _id: string | number
  name?: string
  avatar?: string | number | (() => React.ReactNode)
}

📖 Props Reference

Core Configuration

Refs

Keyboard & Layout

Understanding keyboardVerticalOffset

The keyboardVerticalOffset tells the KeyboardAvoidingView where its container starts relative to the top of the screen. This is essential when GiftedChat is not positioned at the very top of the screen (e.g., when you have a navigation header).

Default value: insets.top (status bar height from useSafeAreaInsets()). This works correctly only when GiftedChat fills the entire screen without a navigation header. If you have a navigation header, you need to pass the correct offset via keyboardAvoidingViewProps.

What the value means: The offset equals the distance (in points) from the top of the screen to the top of your GiftedChat container. This typically includes:

How to use:

import { useHeaderHeight } from '@react-navigation/elements'

function ChatScreen() {
  // useHeaderHeight() returns status bar + navigation header height on iOS
  const headerHeight = useHeaderHeight()

  return (
    <GiftedChat
      keyboardAvoidingViewProps={{ keyboardVerticalOffset: headerHeight }}
      // ... other props
    />
  )
}

Note: useHeaderHeight() requires your chat component to be rendered inside a proper navigation screen (not conditional rendering). If it returns 0, ensure your chat screen is a real navigation screen with a visible header.

Why this matters: Without the correct offset, the keyboard may overlap the input field or leave extra space. The KeyboardAvoidingView uses this value to calculate how much to shift the content when the keyboard appears.

Using your own KeyboardProvider

By default GiftedChat wraps itself in a KeyboardProvider with statusBarTranslucent and navigationBarTranslucent enabled. On some Android/Expo (edge-to-edge) setups this can cause the screen to shift down, flicker on mount, or distort a navigation header - especially when another KeyboardProvider already exists higher in the tree.

Set disableKeyboardProvider to skip the built-in wrapper:

import { KeyboardProvider } from 'react-native-keyboard-controller'

// Mount a single KeyboardProvider once, near the root of your app
function App() {
  return (
    <KeyboardProvider>
      {/* ...navigation / screens... */}
    </KeyboardProvider>
  )
}

function ChatScreen() {
  return (
    <GiftedChat
      disableKeyboardProvider
      // ... other props
    />
  )
}

Important: GiftedChat's keyboard avoidance relies on a KeyboardProvider being present in the tree. When you set disableKeyboardProvider, you must mount your own KeyboardProvider above GiftedChat - otherwise the keyboard will cover the input. Mounting exactly one provider (yours) is what avoids the double edge-to-edge shift.

Text Input & Composer

Actions & Action Sheet

Messages & Message Container

Message Bubbles & Content

react-native-gifted-chat (FaridSafi/react-native-gifted-chat)

Searching Database…

这是什么?react-native-gifted-chat 是一个高度可定制的 React Native 聊天界面组件库,内置支持 React Native Web 和 Expo。开箱即用,包含头像、正在输入提示、表情回应、快速回复等功能。
这是一个独立的聊天应用,还是需要添加到我自己应用里的东西?它是一个 React Native 组件库,你需要安装它,并把它放进自己应用的页面中,它不是一个独立的聊天应用。
如何把它添加到 React Native 项目中?使用 yarn 或 npm 安装该包及其对等依赖,为 iOS 运行 `pod install`,并按照 react-native-reanimated 自身的安装指南配置 Babel 插件。Expo 项目的安装方式略有不同,详见 Expo 部分。
它需要哪些 React Native/iOS/Android 版本?需要 React Native 0.70+、iOS 13.4+、Android API 21+(Android 5.0);如果使用 Expo,则需要 Expo SDK 50+;TypeScript 5.0+ 为可选。
这个项目还在积极开发吗?不了,项目现已进入维护模式,不再接收新功能;维护者建议大家转用社区分支 @kesha-antonov/react-native-chat 以继续开发,迁移基本上只需要改个名字。
它也支持网页吗,还是只支持 iOS 和 Android?它支持 iOS 和 Android,并可通过 react-native-web 运行在网页上,也能轻松集成到 Expo 项目中。
我能自定义聊天界面的各个部分吗?几乎每个部分都可以通过 render* 属性替换为你自己的组件,包括气泡、输入框、发送按钮、头像、操作面板,以及文本、图片、视频和音频消息的渲染器。
它支持已读回执和正在输入提示吗?支持。它包含正在输入提示,以及基于对勾的状态指示(已发送、已送达、已读),还支持滑动回复、表情回应和类似机器人的快速回复按钮。
屏幕键盘弹出时会发生什么?GiftedChat 会自动用 KeyboardProvider 包裹自身,以实现键盘自动避让。但在某些 Android/Expo 边到边(edge-to-edge)的设置下,这可能导致闪烁或布局偏移。此时你可以禁用内置的 provider,改为自己挂载一个。
Creator
FaridSafi
Size
52.5 MB
Stars
14.4K
Age
10y
Commits · 90 Days