簡體   English   中英

我的 chartJS axios.get 被觸發兩次,導致圖表也渲染兩次,我不知道為什么。 我正在使用 react-chartjs-2

[英]My chartJS axios.get is fired twice causing chart to render twice too and i don't know why. I'm using react-chartjs-2

我遇到了一個問題,我的 api 調用被觸發兩次而不是一次,因此它也渲染了 2 個圖表。 為什么?

export default class Graph extends Component {
  constructor(props) {
    super(props);
this.state: {
humidity_data
}

axios_humidity_data = () => {
    return new Promise((resolve, reject) => {
      try {
        axios
          .get(
            `url'`
          )
          .then(response => {
            const data = response.data.results[0].series[0].values;
            let date = [];
            let humidity = [];
            data.forEach(chart_item => {
              date.push(chart_item[0]);
              humidity.push(chart_item[1]);
            });
            this.setState({
              humidity_data: {
                labels: date,
                datasets: [
                  {
                    label: "Humidity %",
                    data: humidity,
                  }
                ]
              }
            });
            console.log(data);
            resolve(data);
          });
      } catch (err) {
        reject(err);
      }
    });
  };

  componentDidMount() {
    this.axios_humidity_data();
  }

  render() {
    return (
      <div>
        <Line
          data={this.state.pressure_data}
          options={{
            title: {
              display: true,
              text: "Pressure kPa",
            },
          }}
        />
      </div>
    );
  }
}

我預計只會觸發一個調用並呈現一個圖表,但它會觸發兩次並呈現 2 個圖表。

使用這種方法:

export default class Graph extends Component {
    constructor(props) {
        super(props);
        this.state: {
            humidity_data
        }
          var s = true;
        if (s) {
            axios_humidity_data = () => {
                return new Promise((resolve, reject) => {
                    try {
                        axios
                            .get(
                                `url'`
                            )
                            .then(response => {
                                const data = response.data.results[0].series[0].values;
                                let date = [];
                                let humidity = [];
                                data.forEach(chart_item => {
                                    date.push(chart_item[0]);
                                    humidity.push(chart_item[1]);
                                });
                                this.setState({
                                    humidity_data: {
                                        labels: date,
                                        datasets: [
                                            {
                                                label: "Humidity %",
                                                data: humidity,
                                            }
                                        ]
                                    }
                                });
                                console.log(data);
                                resolve(data);
                            });
                    } catch (err) {
                        reject(err);
                    }
                });
            };
        }
        s = false;
        componentDidMount() {
            this.axios_humidity_data();
        }

        render() {
            return (
                <div>
                <Line
            data={this.state.pressure_data}
            options={{
                title: {
                    display: true,
                        text: "Pressure kPa",
                },
            }}
            />
            </div>
        );
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM