Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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
Reactjs 组件将在设置状态的函数上安装警告_Reactjs - Fatal编程技术网

Reactjs 组件将在设置状态的函数上安装警告

Reactjs 组件将在设置状态的函数上安装警告,reactjs,Reactjs,我的渲染方法中有一个简单的清除按钮 <Button onClick={this.clearFilters(data)}> CLEAR </Button> 但是如果我加上setState行,我会收到大量警告,它会冻结 警告:无法在现有状态转换期间更新,例如在渲染或其他组件的构造函数中。渲染方法应该是道具和状态的纯函数;构造函数的副作用是一种反模式,但可以移动到componentWillMount 如何在componentWillMount中实现此功能?我建议您将此清除更改

我的渲染方法中有一个简单的清除按钮

<Button onClick={this.clearFilters(data)}> CLEAR </Button>
但是如果我加上setState行,我会收到大量警告,它会冻结

警告:无法在现有状态转换期间更新,例如在渲染或其他组件的构造函数中。渲染方法应该是道具和状态的纯函数;构造函数的副作用是一种反模式,但可以移动到componentWillMount


如何在componentWillMount中实现此功能?

我建议您将此清除更改为{this.clearFiltersdata}}>CLEAR。 我认为您的button onClick方法在渲染时试图执行clearFilters函数,因此错误

this.clearFilterdata立即被执行;这是一个函数调用。当用户点击时,它不会发生

将其更改为。clearFiltersdata}>CLEAR

此外,为什么要在componentWillMount中执行此操作?为什么不先将状态设置为应该的状态?类似于

constructor(props) {
  super(props);
  this.state = { ... };
}

祝你好运

这是因为无限循环,onClick需要一个函数,使用这个:onClick={=>this.clearFiltersdata}是的……修复了它。Thanks@EvilEddie那么请接受这个答案
constructor(props) {
  super(props);
  this.state = { ... };
}