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

React Native中react-native-vision-camera的Camera组件无法作为JSX组件使用问题

解决react-native-vision-camera Camera组件JSX类型错误

可能的原因及解决方案

1. 清理依赖缓存

缓存冲突常导致类型定义异常,执行以下操作:

  • 删除项目根目录下的node_modules文件夹、package-lock.json(或yarn.lock)
  • 重新安装依赖:npm install 或 yarn install
  • 清除Metro缓存:npx react-native start --reset-cache

2. 对齐TypeScript类型依赖版本

类型包版本不匹配是常见诱因:

  • 更新@types/react和@types/react-native到与项目React、React Native兼容的最新版本:
npm install @types/react@latest @types/react-native@latest --save-dev
  • 确保tsconfig.json中关键配置正确:
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "allowSyntheticDefaultImports": true
  }
}

3. 调整Camera组件导入方式

部分版本的react-native-vision-camera中,Camera组件需通过默认导入获取:

// 修改导入语句
import Camera from "react-native-vision-camera";
import { useCameraPermission, useCameraDevice, useCodeScanner } from "react-native-vision-camera";

4. 验证组件属性传递

确保Camera组件必填属性完整,补充样式避免布局问题:

import { StyleSheet } from "react-native";

// ...其他代码

return (
  <View style={StyleSheet.absoluteFill}>
    {device && <Camera device={device} codeScanner={codeScanner} style={StyleSheet.absoluteFill} />}
  </View>
);

额外提示

别忘了处理相机权限逻辑,在无权限时触发请求:

if (!hasPermission) {
  return (
    <View>
      <Button title="请求相机权限" onPress={requestPermission} />
    </View>
  );
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 05:12:20