【发布时间】:2017-05-16 21:23:05
【问题描述】:
我需要设置状态并在 onClick 事件上运行函数。我如何在 React 中实现这一点?
我试图将这两件事放在their own function 中,但这在下面给了我这个警告。
警告:setState(...):在现有状态转换期间无法更新(例如在
render或其他组件的构造函数中)。渲染方法应该是 props 和 state 的纯函数;构造函数副作用是一种反模式,但可以移至componentWillMount。
editLink(onEditLink, link) {
this.setState({showContent: false});
onEditLink(link);
}
render() {
return (
<a onClick={this.editLink(this.props.onEditLink, this.props.link)}>Edit</a>
)
}
【问题讨论】:
标签: javascript reactjs