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

react-native-paper SegmentedButtons组件引入报错问题求助

解决react-native-paper SegmentedButtons组件导入报错问题

复制react-native-paper官方SegmentedButtons示例代码到本地项目后,触发如下错误:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

移除该组件代码后项目可正常运行,但相同代码在Expo Snack中无报错且能正常渲染。本地核心代码如下:

import {SegmentedButtons} from 'react-native-paper'; 
const [value, setValue] = useState(''); 

return ( 
  <SafeAreaView> 
    <SegmentedButtons 
      value={value} 
      onValueChange={setValue} 
      buttons={[ 
        { value: 'walk', label: 'Walking', }, 
        { value: 'train', label: 'Transit', }, 
        { value: 'drive', label: 'Driving', }, 
      ]}
    > 
    </SegmentedButtons> 
  </SafeAreaView> 
)

可能的解决方案:

  • 版本不匹配:Expo Snack中使用的react-native-paper版本大概率比本地项目的新,SegmentedButtons在旧版本中可能未被添加为独立命名导出。查看Snack依赖中的react-native-paper版本,执行npm install react-native-paper@对应版本或yarn add react-native-paper@对应版本升级本地依赖。
  • 缺失PaperProvider配置:react-native-paper的组件必须在PaperProvider包裹的上下文环境中运行,Snack会自动配置,但本地项目可能遗漏。在App根组件添加包裹:
import { PaperProvider } from 'react-native-paper';

export default function App() {
  return (
    <PaperProvider>
      {/* 你的页面组件层级 */}
    </PaperProvider>
  );
}
  • 清除项目缓存:本地缓存可能导致组件导出识别异常,执行对应命令清除:
    • React Native项目:npx react-native start --reset-cache
    • Expo项目:npx expo start -c
  • 验证导入正确性:查看本地node_modules/react-native-paper/src/components/SegmentedButtons下的导出文件,确认组件是命名导出还是默认导出,调整导入语句匹配实际导出方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:40:33