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

使用React Native Gifted Chat报错:undefined is not a function求助

解决React Native Gifted Chat的TypeError: undefined is not a function错误

这个错误的核心原因是你在渲染<GiftedChat>组件时,没有传入它运行必需的核心props,导致内部MessageContainer组件尝试调用未定义的函数。

直接修复方法

你需要给<GiftedChat>传入至少三个关键props:messages、user和onSend,对应你代码里已经定义好的状态和函数。修改你的return部分代码如下:

return (
  <ImageBackground 
    resizeMode="cover" 
    source={require("../assets/chatbg.png")}
    style={{flex: 1}}
  >
    <GiftedChat
      messages={messages}
      user={senderUser}
      onSend={onSend}
    />
  </ImageBackground>
)

额外排查方向(若上述修复无效)

  • 检查react-native-gifted-chat版本是否与你的React Native版本兼容,比如最新版可能需要React Native 0.60+,版本不匹配时可以尝试降级或升级包版本
  • 清理项目缓存并重新安装依赖:
    # 清除npm缓存
    npm cache clean --force
    # 删除node_modules和锁文件
    rm -rf node_modules package-lock.json
    # 重新安装依赖
    npm install
    # 重置Metro缓存
    npx react-native start --reset-cache
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 05:29:55