Answer the question
In order to leave comments, you need to log in
How to close toggle?
How to close toggle when clicking anywhere?
So that the closure works wherever I click, and not just on the same div
constructor(props) {
super(props);
this.state = {
etc_dropdown_toggle: false
}
}
handle_etc_dropdown_toggle = () => {
this.setState(prevstate => {
return {
etc_dropdown_toggle:
prevstate.etc_dropdown_toggle === true ? false : true
}
})
}
<div className='etc_control' onClick={this.handle_etc_dropdown_toggle}></div>
Answer the question
In order to leave comments, you need to log in
Subscribe to the window's click event.
https://codesandbox.io/s/w2orr6nmzl
probably forward props from parent components, and already there catch onClick and save like toggleOpened = true ... well, or Redux, although if there are a lot of these toggles on the page, there is probably another method more elegant
We draw a transparent div under the button with position: "fixed" and stretch it to the entire page, by clicking on it we call toggle
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question