You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native使用GiftedChat报RNCSafeAreaProvider重名注册错误

问题背景

在未使用Expo的React Native CLI项目中,导入使用react-native-gifted-chat库的GiftedChat组件时触发报错:

Invariant Violation: Tried to register two views with the same name RNCSafeAreaProvider, js engine: hermes

已尝试过react-native-safe-area-context相关issue中列出的多套解决方案,已知多包依赖不同版本react-native-safe-area-context会触发同类问题,已完成以下排查:

  1. 将react-native-safe-area-context升级至发帖时最新的4.2.5版本,问题未解决
  2. 尝试卸载react-native-safe-area-context后全量清缓存重装依赖,执行步骤为:
    • 执行rm node_modules删除项目依赖目录
    • 执行rm -rf ios/Pods && ios/Podfile.lock清除iOS端Pods缓存与锁文件
    • 执行rm package-lock.json删除npm依赖锁文件
    • 执行npm install重装npm依赖
    • 执行npx pod-install重装iOS端Pods依赖
    • 重新构建项目后报错依旧
      当前判断问题与react-native-gifted-chat包相关,项目相关依赖版本:
  • react-native: 0.66.4
  • react-native-safe-area-context: ^3.1.9
  • react-native-gifted-chat: ^1.0.0
修复方案

报错根因是项目中存在两份不同版本的react-native-safe-area-context同时被原生端注册,你之前的清理操作存在命令参数错误,加上版本未对齐,导致问题一直存在,按以下步骤操作即可解决:

  1. 先执行npm ls react-native-safe-area-context打印全依赖树,确认react-native-gifted-chat依赖的react-native-safe-area-context具体版本号
  2. 对齐版本:RN 0.66版本最高适配react-native-safe-area-context3.x系列,你之前升4.2.5本身就存在版本兼容问题,且react-native-gifted-chat@1.0.0默认依赖的也是3.x版本,直接把项目里的react-native-safe-area-context版本固定为3.3.2(3.x系列适配RN0.66的稳定版)
  3. 在项目package.json中添加resolutions字段,强制全项目所有依赖都使用同一个版本的react-native-safe-area-context,避免多版本共存:
{
  "resolutions": {
    "react-native-safe-area-context": "3.3.2"
  }
}
  1. 修正之前的清理命令,补全缺失参数,彻底清除旧缓存后重装依赖:
    • 执行rm -rf node_modules(原命令缺-rf参数,无法删除目录)
    • 执行rm -rf ios/Pods ios/Podfile.lock(原命令&&后缺rm指令,实际没删掉锁文件)
    • 执行rm package-lock.json
    • 执行npm install
    • 执行cd ios && pod install --repo-update
    • 回到项目根目录执行npx react-native start --reset-cache启动带缓存清理的Metro服务,再重新编译项目即可

内容的提问来源于stack exchange,提问作者Javier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 01:27:26