简体   繁体   English

将react.js与chart.js一起使用时出现“ TypeError:document.getElementById(…)为空”

[英]Getting “TypeError: document.getElementById(…) is null” when using react.js with chart.js

I am using react and chart.js together to plot bar chart. 我一起使用react和chart.js绘制条形图。 But in chart.js we have to use canvas tags and to find that tag and to place bar chart in it we use our normal document.getElementById() method. 但是在chart.js中,我们必须使用canvas标记并找到该标记并将条形图放入其中,我们使用常规的document.getElementById()方法。 I tried lots of combinations like putting document.getElementById() at the beginning, or inside a $(document).ready() , even within react's componentDidMount() method. 我尝试了很多组合,例如将document.getElementById()放在开头或$(document).ready() ,甚至在react的componentDidMount()方法中。 But Still I am getting " TypeError: document.getElementById(...) is null ". 但是,我仍然收到“ TypeError: document.getElementById(...) is null ”。 If anyone know about it, please give me a suggestion. 如果有人知道,请给我一个建议。 Thank You. 谢谢。

Here is the code which I am trying to execute: 这是我要执行的代码:

var React = require("react");

var Chart = React.createClass({

    getDefaultProps: function() {
        barChartData = {
            labels : ["Option1", "Option2"],
            datasets : [
                {
                    fillColor : "rgba(220,220,220,0.5)",
                    strokeColor : "rgba(220,220,220,0.8)",
                    highlightFill: "rgba(220,220,220,0.75)",
                    highlightStroke: "rgba(220,220,220,1)",
                    data : [65, 35]
                }
            ]
        }
    },

    onload: function() {
        console.log(document.getElementById('canvas_poll'));
        var ctx = document.getElementById("canvas_poll").getContext("2d");

        window.myBar = new Chart(ctx).HorizontalBar(this.props.barChartData, {
            responsive : true,
        });
    },

    componentDidMount: function() {
        this.onload();
    },

    render: function() {

        return (
            <div>
                <canvas classID="canvas_poll" height={100} width={600}></canvas>
            </div>
        );
    }

});

module.exports = Chart;

The document.getElementById() uses an element's 'ID' attribute, not 'classID' (which is also unsupported by the 'canvas' tag). document.getElementById()使用元素的“ ID”属性,而不是“ classID”(“ canvas”标记也不支持)。 Try adding id="canvas_poll" to the canvas element like so: 尝试将id="canvas_poll"添加到canvas元素,如下所示:

<canvas id="canvas_poll" height={100} width={600}></canvas>

I had a similar problem with a react web app I was building. 我正在构建的React Web应用程序也有类似的问题。 In addition to what was said in the previous solution, there are a few things I think you might find helpful based on what I read in the comments. 除了先前解决方案中所说的内容之外,根据我在注释中所读内容,我认为您可能还会发现一些有用的内容。

Script is ran before the element actually exists 在元素实际存在之前运行脚本

One common problem is that the element does not exist at the time of rendering. 一个普遍的问题是在渲染时该元素不存在。

for example if your HTML looked like 例如,如果您的HTML看起来像

    <script src="**your file **" type="text/babel"></script>
    <div id="canvas_poll"></div>

then your script would be ran before the div with the id you are looking for could get rendered. 那么您的脚本将在具有您要查找的ID的div之前运行。 However, when switched, the script is ran after the div is formed. 但是,切换时,脚本会在div形成后运行。

    <div id="canvas_poll"></div>
    <script src="**your file **" type="text/babel"></script>

That way, the element that the script is looking for actually exists and can be found. 这样,脚本正在寻找的元素实际上存在并且可以找到。

Some solutions to this are listed in this link Why does jQuery or a DOM method such as getElementById not find the element? 此链接中列出了一些解决方案, 为什么jQuery或诸如getElementById之类的DOM方法找不到元素?

Change <div /> to <div></div> 将<div />更改为<div> </ div>

In my case, I was still getting null from my getElementById method after running the script after the div was formed. 就我而言,在div形成后运行脚本后,我仍无法从getElementById方法获取null。 It ended up being how the target divs were set up. 最终是如何设置目标div。

I'm not sure why, but when my target div looked like 我不确定为什么,但是当我的目标div看起来像

    <div id= "target1"/>
    <script src="target1.jsx" type="text/babel"></script>

I could render to that container using react. 我可以使用react渲染到那个容器。 However, if I tried to render a second react element: 但是,如果我尝试渲染第二个react元素:

    <div id= "target1"/>
    <script src="target1.jsx" type="text/babel"></script>
    <div id= "target2"/>
    <script src="target2.jsx" type="text/babel"></script>

The first react element would show up but the second would not. 第一个react元素将显示,而第二个则没有。 What I did to fix it was change the div formatting from 我要做的是将div格式从更改为

     <div id="target"/>

to

     <div id="target></div>

on all the target divs. 在所有目标div上

After I made that change, all my react elements came in fine. 完成更改后,我所有的反应要素都变得很好。 I'm not sure why it worked that way, but I hope it is helpful. 我不确定为什么会这样,但是我希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM