React Native 0.66集成react-native-pie-chart报ArtShape缺失怎么解决?
报错原因
Required Native component ArtShape was not found in the UIManager 报错的根本原因是:react-native-pie-chart@2.0.2 依赖的ART绘图库已从React Native 0.60及以上版本的核心包中移除,项目中缺少对应的原生模块导致无法识别ArtShape组件。
解决步骤
- 第一步:安装社区维护的ART依赖包
执行如下命令安装依赖:
# 使用npm npm install @react-native-community/art --save # 或使用yarn yarn add @react-native-community/art
- 第二步:安装iOS端原生依赖
进入项目ios目录,执行pod安装命令:
cd ios && pod install && cd ../
执行完成后关闭当前运行的Metro服务和iOS模拟器,重新编译运行项目。
- 第三步:修正代码语法错误
你当前的类组件代码存在语法问题:类主体内不允许直接用const声明变量,你已经在render函数内部定义了所需的widthAndHeight、series、sliceColor变量,直接删除类主体内冗余的3行const声明即可。修正后的代码如下:
import PieChart from 'react-native-pie-chart'; import {Component, ScrollView, View, Text, StyleSheet} from 'react-native'; export default class DashoardChart extends Component { render() { const widthAndHeight = 250; const series = [123, 321, 123, 789, 537]; const sliceColor = ['#F44336', '#2196F3', '#FFEB3B', '#4CAF50', '#FF9800']; return ( <ScrollView style={{flex: 1}}> <View style={styles.container}> <Text style={styles.title}>Basic</Text> <PieChart widthAndHeight={widthAndHeight} series={series} sliceColor={sliceColor} /> <Text style={styles.title}>Doughnut</Text> <PieChart widthAndHeight={widthAndHeight} series={series} sliceColor={sliceColor} doughnut={true} coverRadius={0.45} coverFill={'#FFF'} /> </View> </ScrollView> ); } } const styles = StyleSheet.create({ container: { alignItems: 'center', padding: 20, }, title: { fontSize: 18, marginVertical: 10, } });
完成以上操作后即可正常渲染饼图和环形图。
内容的提问来源于stack exchange,提问作者Janvier
相关产品推荐
相关产品推荐

