【发布时间】:2021-06-30 23:30:06
【问题描述】:
在render方法中,为什么不喜欢onClick属性中的this.increment?我不确定最大更新深度误差是如何关联的。抱歉,我是新来的反应,找不到太多关于此的文档。
import React, { Component } from 'react'
class Counter extends Component {
constructor(props) {
super(props)
this.state = {
count: 0
}
}
increment() {
this.setState(
{
count: this.state.count + 1
},
() => {
console.log('Callback value', this.state.count)
}
)
}
render() {
return (
<div>
<div> Count - {this.state.count}</div>
<button onClick={this.increment()}> Increment </button>
</div>
)
}
}
export default Counter
【问题讨论】:
-
将此
onClick={this.increment()}更改为onClick={this.increment}应该可以解决它
标签: reactjs