Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应:在调用第二次ComponentWillReceiveProps后运行方法_Javascript_Reactjs_Ag Grid_Ag Grid React - Fatal编程技术网

Javascript 反应:在调用第二次ComponentWillReceiveProps后运行方法

Javascript 反应:在调用第二次ComponentWillReceiveProps后运行方法,javascript,reactjs,ag-grid,ag-grid-react,Javascript,Reactjs,Ag Grid,Ag Grid React,我的react应用程序有一个主ag网格。单击按钮(提供新数据)时,它将重新呈现 我想添加一个预过滤器,最初过滤数据。因此,我创建了一个新方法来创建我的预过滤器模型,然后应用它: initialFilter(){ ... this.state.gridOptions.api.setFilterModel(myView); this.state.gridOptions.api.onFilterChanged(); } 我从测试中知道这是可行的(我在选择行并应用过滤器时使用的

我的react应用程序有一个主ag网格。单击按钮(提供新数据)时,它将重新呈现

我想添加一个预过滤器,最初过滤数据。因此,我创建了一个新方法来创建我的预过滤器模型,然后应用它:

initialFilter(){
    ...
    this.state.gridOptions.api.setFilterModel(myView);
    this.state.gridOptions.api.onFilterChanged();
}
我从测试中知道这是可行的(我在选择行并应用过滤器时使用的方法上调用了该方法)

问题是我把这个方法称为哪里

将其放入ComponentWillReceiveProps会导致一个无限循环(我假设是由于上面提到的两行代码)

我只需要运行该方法一次(每次单击按钮)。经过一番探索,我注意到如果我在

componentWillRecieveProps(){
...
   if (counter <= 1){
      this.initialFilter();
      counter++;
   }
}
componentwillreceiveprops(){
...

如果(counter我已经设法用稍微好一点的方法修复了它:

在ComponentSwillReceiveProps中,我添加了以下内容:

if (props.preFilter){
    this.initialFilter();
    counter++;
    if (counter == 2){
        props.preFilterWasApplied();
        counter=0;
    }
} 
在父组件中,我设置了一个新状态,用于切换是否调用此if语句。单击按钮时,状态切换为true。计数器最初设置为0。initialFilter方法将运行并被忽略(?),但允许我再次调用组件SwillReceiveProps。这将导致initialFilter再次运行(这次不能忽略)和要设置为2的计数器。这将通过第二个if语句并调用父方法。父方法只需将状态切换回false。计数器将重置回0,以便下一次单击按钮

该状态将继续保持为false,直到我再次单击按钮,并且在第二个componentsWillReceiveProps调用后,它将再次设置为false


如果有人有更优雅的方法来完成上述工作,那将不胜感激:)

找到更优雅的解决方案:只需将其全部复制到shouldComponentUpdate中,并删除计数器+内部If语句!!!