首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Expo中使用react-native-svg-charts修复不变冲突:元素类型无效

如何在Expo中使用react-native-svg-charts修复不变冲突:元素类型无效
EN

Stack Overflow用户
提问于 2019-02-12 10:51:57
回答 1查看 1.4K关注 0票数 1

我正在尝试使用本例中的react-native-svg-charts向我的面积图添加一个梯度:https://github.com/JesperLekland/react-native-svg-charts-examples/blob/master/storybook/stories/area-chart/with-gradient.js

代码可以编译,但我在运行时得到以下错误:

https://imgur.com/nasKm9x (抱歉,我还没有足够的名气来发布图片)

我正在使用我的Android手机上的Expo。我对编程相当陌生,所以我甚至不确定该尝试什么。

代码语言:javascript
复制
import * as shape from 'd3-shape'
import { View } from 'native-base';
import * as React from 'react';
import { AreaChart, Defs, LinearGradient, Path, Stop } from 'react-native-svg-charts'
// import styles from './styles';

class DashboardChart extends React.Component {
  render() {
    const data = [ 50, 10, 40, 95, 14, 24, 85, 91, 35, 53, 53, 24, 50, 20, 80 ]

        const ChartLine = ({line}) => (
            <Path
                key={'line'}
                d={line}
                stroke={'rgb(134, 65, 244)'}
                fill={'none'}
            />
        )

        const Gradient = ({ index }) => (
          <Defs key={index}>
              <LinearGradient id={'gradient'} x1={'0%'} y={'0%'} x2={'0%'} y2={'100%'}>
                  <Stop offset={'0%'} stopColor="blue" stopOpacity={1}/>
                  <Stop offset={'100%'} stopColor="white" stopOpacity={1}/>
              </LinearGradient>
          </Defs>
      )

    return (
          <View>
            <AreaChart
                style={{ height: 200 }}
                data={data}
                contentInset={{ top: 40, bottom: 10 }}
                curve={shape.curveNatural}
                svg={{
                  fill: 'url(#gradient)'
                }}
            >
                {/* <Grid/> */}
                <Gradient/>
                <ChartLine/>
            </AreaChart>
          </View>
    );
  }

}

export default DashboardChart;
EN

回答 1

Stack Overflow用户

发布于 2019-02-16 05:35:35

在您链接到DefsLinearGradientStop组件的示例中,这些组件是从react-native-svg导入的。因此,这些组件可能是react-native-svg-charts库中的undefined,这可以解释您看到的错误。

尝试将您的导入修复为此(如果尚未安装react-native-svg,请安装它):

代码语言:javascript
复制
import { AreaChart } from 'react-native-svg-charts'
import { Defs, LinearGradient, Path, Stop } from 'react-native-svg'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54642152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档