ChartJS道具数据未定义错误ReactJS

问题描述

我正在尝试将Jira门票数据传递到饼图组件中。但是,我遇到此错误

 Failed prop type: The prop `data` is marked as required in `ChartComponent`,but its value is `undefined`

请忽略: 看起来您的帖子大部分是代码;请添加更多详细信息。 看起来您的帖子大部分是代码;请添加更多详细信息。 看起来您的帖子大部分是代码;请添加更多详细信息。 看起来您的帖子大部分是代码;请添加更多详细信息。 看起来您的帖子大部分是代码;请添加更多详细信息。

Error

import React,{ useContext,useEffect,useState } from 'react'
import { LoadingContext } from '../../../context/loading-context'
import { SnackbarContext } from '../../../context/snackbar-context'
import { UserContext } from '../../../context/user-context'
import {get} from '../../../functions/Rest'
import { Pie } from 'react-chartjs-2'

const defects = props => {

    const loading = useContext(LoadingContext)
    const snackbar = useContext(SnackbarContext)
    const user = useContext(UserContext)
  

    const [jiraticketsData,setJiraTicketsData] = useState('')


    useEffect(() => {
        async function onLoadJiraTicketData() {
          loading.setLoading(true)
          const results = get(`get_jira_tickets`,user.user)
          if (results.status === 0) {
              setJiraTicketsData(results.data)
          } else if (results.status >= 20 && results.status <= 30 ){
            snackbar.statusCheck(results)
            user.setSessionTokenMatches(false)
          } else {
            snackbar.statusCheck(results)
          }
          loading.setLoading(false)
        }
        onLoadJiraTicketData()

      },[])


  return (
    <div className='defects'>
      <Pie data={jiraticketsData.pie_chart} />
      </div>
  )
}


export default defects

我正在尝试访问pie_chart数组数据:

Pie chart array

这是ChartComponent代码,如果需要的话:

import * as React from "react";
import * as chartjs from "chart.js";

export type ChartDataFunction<T extends chartjs.ChartData> = (
  element: HTMLElement
) => T;
export type ChartData<T extends chartjs.ChartData> = ChartDataFunction<T> | T;

export interface ChartComponentProps {
  data: ChartData<chartjs.ChartData>;
  type?: chartjs.ChartType;
  getDatasetAtEvent?(e: any): void;
  getElementAtEvent?(e: any): void;
  getElementsAtEvent?(e: any): void;
  height?: number;
  legend?: chartjs.ChartLegendOptions;
  onElementsClick?(e: any): void; // alias for getElementsAtEvent (backward compatibility)
  options?: chartjs.ChartOptions;
  plugins?: object[];
  redraw?: boolean;
  width?: number;
  datasetKeyProvider?: (any: any) => any;
}

export interface LinearComponentProps extends ChartComponentProps {
  data: ChartData<chartjs.ChartData>;
}

export default class ChartComponent<
  P extends ChartComponentProps
> extends React.Component<P,{}> {
  chartInstance: chartjs;
}

export class Doughnut extends ChartComponent<ChartComponentProps> {}

export class Pie extends ChartComponent<ChartComponentProps> {}

export class Line extends ChartComponent<LinearComponentProps> {}

export class Scatter extends ChartComponent<ChartComponentProps> {}

export class Bar extends ChartComponent<LinearComponentProps> {}

export class HorizontalBar extends ChartComponent<ChartComponentProps> {}

export class Radar extends ChartComponent<ChartComponentProps> {}

export class Polar extends ChartComponent<ChartComponentProps> {}

export class Bubble extends ChartComponent<ChartComponentProps> {}

export var defaults: {
  global: chartjs.ChartOptions & chartjs.ChartFontOptions;
  [key: string]: any;
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)