JSX part looks like this:
<span className="header_srh_i_c" onMouseDown={this.toggleSearchbar}>
<i className="fa fa-search"
id="searchButton"
name="searchButton"
/>
</span>
function in the same component looks like this:
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
})
}
}
state remains false
event after execution of
this.setState({showSearch:!this.state.showSearch},function (event) {
console.log("state after", this.state.showSearch) //false
})
However, it works just fine if I use onClick
instead of onMouseDown
try
this.setState(prev => ({showSearch: !prev.showSearch}), function(){.....})
instead of
this.setState({showSearch:!this.state.showSearch},function (event) {
console.log("state after", this.state.showSearch) //false
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With