首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在React + ReferenceError -chartjs中未定义图表的获取

在React + ReferenceError -chartjs中未定义图表的获取
EN

Stack Overflow用户
提问于 2016-07-19 22:06:22
回答 1查看 1.9K关注 0票数 0

我认为错误来自于我调用了一个空对象或类似的东西?

代码如下

代码语言:javascript
复制
import React from 'react';

export default class ChartGraph extends React.Component {

componentDidMount() {
    let chartCanvas = this.refs.chart;
    let data = this.props.data ? this.props.data : "not vailable data";

    let chartInstance = new Chart(chartCanvas, { // <= [BUG]: Offending line
        type: 'line',
        data: {data},
        options: {
            title: {
              display: true,
              text: 'My awesome chart'
            }
        }   
    });

    this.setState({ chart: chartInstance })
}

componentDidUpdate() {
    let chart = this.state.chart;
    let data = this.props.data;

    data.datasets.forEach((dataset, i) => chart.data.datasets[i].data = dataset.data);

    chart.data.labels = data.labels;
    chart.udpate();
}

render() {

    let cssClassName = this.props.className;

    const chartStyle = {
        width: 600,
        height: 250,
        color: 'white'
    };

    return (

        <canvas
            ref={'chart'}
            style={chartStyle}>
        </canvas>
    );
}

}

错误:

代码语言:javascript
复制
chartgraph.jsx:68Uncaught ReferenceError: Chart is not definedcomponentDidMount @ chartgraph.jsx:68invokeComponentDidMountWithTimer @ ReactCompositeComponent.js:54notifyAll @ CallbackQueue.js:67close @ ReactReconcileTransaction.js:81closeAll @ Transaction.js:204perform @ Transaction.js:151batchedMountComponentIntoNode @ ReactMount.js:126perform @ Transaction.js:138batchedUpdates @ ReactDefaultBatchingStrategy.js:63batchedUpdates @ ReactUpdates.js:98_renderNewRootComponent @ ReactMount.js:285_renderSubtreeIntoContainer @ ReactMount.js:371render @ ReactMount.js:392(anonymous function) @ index.js:11maybeReady @ startup_client.js:26loadingCompleted @ startup_client.js:38
debug.js:41 Exception from Tracker recompute function:
debug.js:41 TypeError: Cannot read property 'chart' of null
    at ChartGraph.componentDidUpdate (chartgraph.jsx:83)
    at ReactCompositeComponentWrapper.invokeComponentDidUpdateWithTimer (ReactCompositeComponent.js:65)
    at CallbackQueue.notifyAll (CallbackQueue.js:67)
    at ReactReconcileTransaction.close (ReactReconcileTransaction.js:81)
    at ReactReconcileTransaction.closeAll (Transaction.js:204)
    at ReactReconcileTransaction.perform (Transaction.js:151)
    at ReactUpdatesFlushTransaction.perform (Transaction.js:138)
    at ReactUpdatesFlushTransaction.perform (ReactUpdates.js:90)
    at Object.flushBatchedUpdates (ReactUpdates.js:173)
    at ReactDefaultBatchingStrategyTransaction.closeAll (Transaction.js:204)
EN

回答 1

Stack Overflow用户

发布于 2016-07-19 23:26:47

问题可能是您从未设置过组件的初始状态,因此当componentDidUpdate尝试引用this.state时,它还不存在。

尝试添加以下内容:

代码语言:javascript
复制
getInitialState() {
    return { chart: null };
}

在类中,您可能需要使用以下代码:

代码语言:javascript
复制
constructor() {
    this.state = { chart: null };
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38460866

复制
相关文章

相似问题

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