C
C
ckatala2019-03-15 13:19:04
React
ckatala, 2019-03-15 13:19:04

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
      }
    })
  }

Further
<div className='etc_control' onClick={this.handle_etc_dropdown_toggle}></div>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Medin K, 2019-03-15
@ckatala

Subscribe to the window's click event.
https://codesandbox.io/s/w2orr6nmzl

H
hzzzzl, 2019-03-15
@hzzzzl

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

D
davidnum95, 2019-03-15
@davidnum95

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 question

Ask a Question

731 491 924 answers to any question