P
P
Par Mactep2016-03-30 18:25:37
React
Par Mactep, 2016-03-30 18:25:37

How to handle click outside react component?

The first thing that comes to mind is a solution

document.addEventListener('click', this.handleOuterClick);

and correspondingly
handleOuterClick: function(e) {
    let $node = $(ReactDOM.findDOMNode(this);
    if ($node.has(e.target).length === 0) {
      /*
      * some actions
      * */
    }
  }

But for some reason it seems that this is a hell of a crutch. Perhaps there is some more beautiful solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2016-03-30
@parmactep

That's pretty much how they do it.
https://www.npmjs.com/package/react-click-outside

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question