JSX 部分如下所示:
<span className="header_srh_i_c" onMouseDown={this.toggleSearchbar}>
<i className="fa fa-search"
id="searchButton"
name="searchButton"
/>
</span>
同一组件中的函数如下所示:
toggleSearchbar(event){
const eventType = event.type;
if(this.state.showSearch && this.props.q && length(this.props.q) > 0){
this.toggleSearching();
}else{
console.log("state after", this.state.showSearch) //false
this.setState({showSearch:!this.state.showSearch},function (event) {
console.log("state after", this.state.showSearch) //false
})
}
}
false
执行后状态保持事件
this.setState({showSearch:!this.state.showSearch},function (event) {
console.log("state after", this.state.showSearch) //false
})
但是,如果我使用onClick
而不是onMouseDown