如何解决超出最大更新深度的问题,在ComponentWillMount中发生错误吗?

问题描述 投票:0回答:1

我已经搜索了此问题,但与此问题相关的案例与我的不同。

我有React client side,其中Im使用react-big-scheduler。在ComponentDidMount中,我必须进行API axios调用并使用响应数据设置状态。

componentWillMount() {
  let today = moment().format(DATE_FORMAT);
  let schedulerData = new SchedulerData(today, ViewTypes.Week);
  schedulerData.localeMoment.locale('en');

  const token = localStorage.getItem("jwttoken");
  axios.post('http://localhost/Auth/api/validate.php', {
      "jwt": token
    }, {
      headers: {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json'
      }
    })
    .then(response => {
      console.log(response.data);
      this.setState({
        vendor: response.data.data
      })
      const vid = response.data.data.VendorID;

      return axios.get('http://localhost/Auth/api/show_all_HR.php?VendorID=' + vid, {
          headers: {
            'Accept': 'application/json, text/plain, */*',
            'Content-Type': 'application/json'
          }
        })
        .then(response => {
          console.log(response.data);
          schedulerData.setResources(response.data);
          this.setState({
            AllHR: response.data,
            viewModel: schedulerData
          });
          console.log("I should be rendered at 1st", this.state.AllHR);
          console.log(vid);
        });
    });



  axios.get('http://localhost/Auth/api/show_one_HR_hours.php?VendorID=48', {
      headers: {
        'Accept': 'application/json, text/plain, */*',
        'Content-Type': 'application/json'
      }
    })
    .then(response => {
      console.log(response.data);
      schedulerData.setEvents(response.data);
      this.setState({
        AllSlots: response.data,
        viewModel: schedulerData
      });
    });

}

现在,我因执行setState而收到此错误:

超出最大更新深度。当一个组件发生这种情况重复调用componentWillUpdate内的setState或componentDidUpdate。 React将嵌套更新的数量限制为防止无限循环。

现在所有其他解决方案表明,当我们在onClick中调用函数或将()放在函数之后时,就会出现问题。但是我没有这样的事情。

所以请建议我在代码中进行哪些更改?

编辑:我这样渲染它:

render() {
    const {viewModel} = this.state;
    return (
      <div>
         <Row style={ {width: "100%"}}>
            <h3> Schedule Maintenance Service Time Slots</h3>
            <Scheduler schedulerData={viewModel}
               prevClick={this.prevClick}
               nextClick={this.nextClick}
               onSelectDate={this.onSelectDate}
               onViewChange={ this.onViewChange}
               eventItemClick={this.eventClicked}
               viewEventClick={this.ops1}
               viewEventText="Ops 1"
               viewEvent2Text="Ops 2"
               viewEvent2Click={this.ops2}
               updateEventStart={this.updateEventStart}
               updateEventEnd={this.updateEventEnd}
               moveEvent={this.moveEvent}
               newEvent={this.newEvent}
               leftCustomHeader={leftCustomHeader}
               toggleExpandFunc={this.toggleExpandFunc}
           />
        </Row>
     </div>
   );
}

prevClick = (schedulerData) => {
  schedulerData.prev();
  schedulerData.setEvents(this.state.AllSlots);
  this.setState({
    viewModel: schedulerData
  })
}

nextClick = (schedulerData) => {
  schedulerData.next();
  schedulerData.setEvents(this.state.AllSlots);
  this.setState({
    viewModel: schedulerData
  })
}

onViewChange = (schedulerData, view) => {
  schedulerData.setViewType(view.viewType, view.showAgenda, view.isEventPerspective);
  schedulerData.setEvents(this.state.AllSlots);
  console.log("Im changing VIEW");

}
javascript reactjs rendering state react-component
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.