【问题标题】:Amchart HTML container not found (React)找不到 Amchart HTML 容器 (React)
【发布时间】:2019-05-13 17:34:15
【问题描述】:

编辑 1(初始):setState() 回调似乎没有按我期望的方式工作。例如:

this.state = {table: true}
this.setState({table: false}, ()=> console.log(this.state.table)); 

将注销“true”,而我希望它注销 false。

编辑 2:我认为我处理下拉请求的方式有问题,这可能会导致我的大部分问题。所以我将把它标记为已解决,因为 Dileep 的解决方案应该有效。我会在几个小时内更新我的结果。

编辑 3(最终):我使用的是 switch() 语句,它以某种方式触发了多个案例并重置了我的状态。

我想我明白问题所在,但是我不完全确定如何解决它。

我想在 Amchart 的 create("elementID", chartType) 函数可以访问之前有条件地渲染一个元素。我希望通过调用以下函数来呈现下拉菜单:

loadBarChart() {
this.setState({ piechart: false, table: false, barchart: true });
var chart = am4core.create("barChartDiv", am4charts.XYChart);
chart.data = this.state.data;

let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "schoolname";
categoryAxis.title.text = "Schools";

let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.title.text = "Number of Students";
}

barChartDiv 显示如下:

 {this.state.barchart && (
      <section>
        <h1 style={{ textAlign: "center" }}>School Data</h1>
        <div id="barChartDiv" style={{ width: "100%", height: "500px" }} />
      </section>
 )}

当我选择下拉时,我被告知barchart为false,然后在出现以下错误后为true:

Instance.js:128 Uncaught Error: html container not found

因为 setState() 通常不会立即更新。我已经尝试通过在 setState() 回调函数中包含 loadBarChart() 代码来解决这个问题,但我得到了与以前相同的错误。所以我几乎可以肯定正在发生的是 am4core.create() 在呈现该元素之前正在寻找一个 id 为“barChartDiv”的元素,所以我得到一个 html container not found 错误。

除了使用 css 之外,最好的处理方法是什么?

【问题讨论】:

    标签: javascript html reactjs charts conditional-rendering


    【解决方案1】:

    我在纯 HTML/JS 中遇到了这个错误。

    发生错误时,我在&lt;div id="chartdiv"&gt;&lt;/div&gt; 之前添加了&lt;script&gt; 部分

    分辨率是将&lt;script&gt;部分放在&lt;div id="chartdiv"&gt;&lt;/div&gt;之后

    写在这里是为了帮助任何可能自己遇到这个问题的人。

    【讨论】:

    • 这是因为他们网站上的一些例子是这样写的
    【解决方案2】:

    以下代码是您的逻辑示例 sn-p,在回调函数中您将能够获取元素。

    请检查解决方案,让我知道您是否遇到任何问题或已解决。

    注意 - 示例代码,但我希望其逻辑相同。打开控制台,执行代码。

    import React, { Component } from 'react';
    import './App.css';
    
    
    class App extends Component {
    
      state = {
        countrySelected: null
      }
    
    
      onCountryChange = (e) => {
        this.setState({
          countrySelected: e.target.value
        }, () => {
            // getting the id
            let id = document.getElementById("barChartDiv");
            console.log("id =>", id)
          })
    
      }
    
    
    
    
      render() {
        return (
          <div>
            <select onChange={this.onCountryChange}>
              <option value="India">India</option>
              <option value="USA">USA</option>
            </select>
            {
              this.state.countrySelected === "India" ?
                <div id="barChartDiv" style={{ width: "100%", height: "500px" }} />
                :
                null
            }
          </div>
        );
      }
    }
    
    
    
    export default App;

    【讨论】:

    • 我实际上是在控制台记录我的 setState 回调并且没有得到任何更改: switch (event.value) { case "barchart": this.setState({ piechart: false, table: false, barchart: true }, () => { console.log(this.state);
    • 状态由于某种原因保持不变
    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 2021-04-12
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多