React - 外部点击组件子树

时间:2017-08-14 16:09:09

标签: reactjs

我知道有几种解决方案可以检测外部'来自React组件的点击 - 即那些基本上使用DOM元素的组件' .contains方法,以查看事件是否源自DOM树 - 但是,是否有人知道任何可以检测到此类事件的内部'实际的组件层次结构?例如,这适用于上述方法不适用的门户(因为DOM树不代表React树)。

编辑:示例:

<div>
  <div onOutsideClick={handleOutsideClick}> // onOutsideClick implementation todo
    <div>
      <button /> // same dom tree, same component tree, would be detected as NOT outside (ding)
    </div>
    <div>
      <Portal>
        <button /> // NOT same dom tree, but same component tree, would be detected as outside (bzzt)
      </Portal>
    </div>
  </div>
  <button /> // not same component tree, nor dom tree, would be detected as outside (ding)
</div>

0 个答案:

没有答案