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

Syncfusion React Sparkline组件报错:_this["render"+data.type]不是函数

解决Syncfusion React Sparkline的TypeError问题

报错信息

TypeError: _this["render" + data.type] is not a function

问题代码

import React from 'react';
import { SparklineComponent, Inject, SparklineTooltip } from '@syncfusion/ej2-react-charts';

const Sparkline = ({ id, height, width, color, currentColor, data, type }) => {
    return (
       <SparklineComponent
          id={id}
          height={height}
          width={width}
          lineWidth={1}
          valueType="Numeric"
          fill={color}
          border={{ color: currentColor, width: 2 }}
          dataSource={data}
          xName="x"
          yName="y"
          type={type}
          tooltipSettings={{
              visible: true,
              format: '${x} : data ${y}', // 此处格式存在冗余
              trackLineSettings: {
                  visible: true
              }
          }}
      >
          <Inject services={[ SparklineTooltip ]} />
      </SparklineComponent>
  );
};

 export default Sparkline;

错误原因与解决方案

核心错误原因

报错本质是传入的type属性值不合法。Syncfusion Sparkline组件仅支持以下固定类型(注意首字母必须大写):

  • Line
  • Column
  • WinLoss
  • Pie
  • Area

若传入的type是小写(如line)、拼写错误或未定义类型,组件内部会尝试调用不存在的render[Type]方法,从而抛出该TypeError。

额外优化点

虽tooltip格式不是当前报错的直接原因,但按文档规范修正后可保证功能正常:

  • 将tooltipSettings.format改为${x} : ${y},去掉冗余的data字样,匹配官方文档格式要求。

验证步骤

  1. 检查父组件传入的type参数值,确保属于上述支持类型且首字母大写;
  2. 确认dataSource中存在x和y字段,与xName、yName配置匹配;
  3. 修正tooltip格式后测试组件功能。

文档参考(翻译自官方文档)

Syncfusion React Sparkline的tooltip功能支持自定义显示格式,通过${字段名}绑定数据源中的字段值;开启trackLineSettings.visible后,tooltip显示时会在图表上展示对应跟踪线,提升交互体验。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 03:54:11