score:6

Accepted answer

you call drawtable in the render method, which causes a fetch request. when this completes, you put the response in state with setstate which causes your component to re-render, and it continues like this indefinitely.

you could instead get the data in componentdidmount and componentdidupdate and just use the data in the render method.

example

class mycomponent extends react.component {
  // ...

  componentdidmount() {
    this.fetchdata();
  }

  componentdidupdate(prevprops) {
    const [, prevday, prevmonth, prevyear] = prevprops.type;
    const [, day, month, year] = this.props.type;

    if (prevday !== day || prevmonth !== month || prevyear !== year) {
      this.fetchdata();
    }
  }

  fetchdata = () => {
    const [, day, month, year] = this.props.type;
    const url = `http://localhost:${react_port}/old/${day}/${month}/${year}`;

    fetch(url)
      .then(response => response.json())
      .then(data => {
        this.setstate({ data });
      })
      .catch(error => {
        console.error(error);
      });
  };

  render() {
    // ...
  }
}

Related Query

More Query from same tag