我的chartJS axios.get被触发两次,导致图表也渲染两次,我也不知道为什么。我正在使用react-chartjs-2

时间:2019-09-23 10:30:05

标签: javascript reactjs chart.js react-chartjs

我遇到了一个问题,我的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张图表。

1 个答案:

答案 0 :(得分:0)

使用这种方法:

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>
        );
        }
    }