Expo 51升级后victory-native报错:Cannot read property 'material' of undefined
解决victory-native升级后组件加载失败及VictoryTheme未定义问题
问题详情
- 系统环境:MacOS
- 依赖版本:
{ "expo": "51.0.12", "react": "18.2.0", "react-native-reanimated": "~3.10.1", "react-native-svg": "^15.3.0", "victory-native": "^41.0.0" }
升级环境后所有victory-native组件无法加载,报错:
ERROR TypeError: Cannot read property 'material' of undefined
打印VictoryTheme发现其值为undefined,相关代码如下:
import { VictoryTheme } from 'victory-native'; import { VictoryChart, VictoryLine, VictoryZoomContainer, VictoryLabel, VictoryAxis, VictoryArea } from 'victory-native'; <VictoryChart theme={VictoryTheme.material} width={screenWidth} height={220} scale={{ x: "time" }} padding={{ left: 20, top: 20, right: 80, bottom: 40 }} containerComponent={<VictoryZoomContainer responsive={false} />} />
解决方案
victory-native v41.x 版本已将主题模块迁移至独立包victory-theme,不再从主包导出VictoryTheme,这是问题的核心原因。按以下步骤修复:
- 安装
victory-theme依赖:
npm install victory-theme # 或使用yarn yarn add victory-theme
- 修改导入路径,从
victory-theme导入VictoryTheme:
import { VictoryTheme } from 'victory-theme'; import { VictoryChart, VictoryLine, VictoryZoomContainer, VictoryLabel, VictoryAxis, VictoryArea } from 'victory-native';
- 重启项目,验证组件是否正常渲染。
你的react-native-svg和react-native-reanimated版本与victory-native v41.x兼容,无需额外调整。
内容的提问来源于stack exchange,提问作者Qinglin Xu
相关产品推荐
相关产品推荐

