为什么mouseDown事件不会在React中传播?

桃木

我有一个简单的React组件,两个绝对位置相同的正方形。蓝色方块覆盖红色方块。

function App() {
  return (
    <div className="App">
      <div>
        <div
          style={{
            position: "absolute",
            backgroundColor: "red",
            width: 100,
            height: 100
          }}
          onMouseDown={() => console.log("Red!")}
        />
        <div
          style={{
            position: "absolute",
            backgroundColor: "blue",
            width: 100,
            height: 100
          }}
          onMouseDown={() => console.log("Blue!")}
        />
      </div>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

将事件侦听器附加到react组件时,只有顶部的组件才能捕获事件,并且不会到达红色方块。为什么会这样?这是默认行为吗?我以为事件传播只停止使用event.stopPropagation()使用香草javascript时,两个方块都会捕获事件。

function clickRed() {
  console.log("Red!");
}

function clickBlue() {
  console.log("Blue!");
}
<div id="root">
    <div style="position: absolute; background-color: red; min-width: 100px; min-height: 100px;" onmousedown="clickRed();" />
    <div style="position: absolute; background-color: blue; min-width: 100px; min-height: 100px;" onmousedown="clickBlue();"/>
</div>

TJ人群

这不是React,而是事件在DOM中的工作方式。问题是事件从最内层的孩子传播到这些孩子的最外祖先,并通过DOM树传播。但是您的元素是兄弟姐妹,没有父子关系。因此,按z顺序位于另一个上方的那个将是接收事件的那个(然后传播到该元素的父对象,而不是其同级对象)。

使用香草javascript时,两个方块都会捕获事件。

与您在问题中定义的DOM结构不同。这是直接的HTML和内联事件处理程序:

<div class="App">
  <div>
    <div
      style="
        position: absolute;
        background-color: red;
        width: 100px;
        height: 100px;
      "
      onmousedown='console.log("Red!")'
    ></div>
    <div
      style="
        position: absolute;
        background-color: blue;
        width: 100px;
        height: 100px;
      "
      onmousedown='console.log("Blue!")'
    ></div>
  </div>
</div>

或直接使用DOM使用JavaScript:

const app = document.createElement("div");
app.innerHTML = `
<div>
  <div
    style="
      position: absolute;
      background-color: red;
      width: 100px;
      height: 100px;
    "
    class="red"
      ></div>
      <div
    style="
      position: absolute;
      background-color: blue;
      width: 100px;
      height: 100px;
    "
    class="blue"
  ></div>
</div>
`;
app.querySelector(".red").addEventListener("mousedown", () => console.log("Red!"));
app.querySelector(".blue").addEventListener("mousedown", () => console.log("Blue!"));
document.getElementById("root").appendChild(app);
<div id="root"></div>


重新编辑并添加以下示例:

function clickRed() {
  console.log("Red!");
}

function clickBlue() {
  console.log("Blue!");
}
<div id="root">
    <div style="position: absolute; background-color: red; min-width: 100px; min-height: 100px;" onmousedown="clickRed();" />
    <div style="position: absolute; background-color: blue; min-width: 100px; min-height: 100px;" onmousedown="clickBlue();"/>
</div>

那里的HTML不正确。在HTML中, 它<div/><div>-只是一个开始标记完全相同结果,您的div元素被嵌套(蓝色在红色里面)。相当于JSX的HTML<div/><div></div>

function clickRed() {
  console.log("Red!");
}

function clickBlue() {
  console.log("Blue!");
}
<div id="root">
    <!-- Scroll right in the stack snippet to see the difference at the end ⇒ ⇒                                             ⇩⇩⇩⇩⇩⇩⇩ -->
    <div style="position: absolute; background-color: red; min-width: 100px; min-height: 100px;" onmousedown="clickRed();"  ></div>
    <div style="position: absolute; background-color: blue; min-width: 100px; min-height: 100px;" onmousedown="clickBlue();"></div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么jQuery中的'change'事件不会在ReactJS中触发onChange?

为什么InputText的onChange事件不会在Blazor中触发?

为什么平台就绪事件不会在电容器中触发?

为什么Empty Publisher 不会在Combine 中触发完成事件?

为什么InternetExplorer 11不会在非活动选项卡中触发事件?

JavaScript 变量不会在 mouseDown() 事件中保留值

为什么我的简单元素不会在此React渲染方法中渲染?

为什么在新的react-router-dom <Redirect />中不会在setTimout内部触发?

为什么我的Animated API组件不会在React Native中显示?

为什么不会在选择框中填充字段?

为什么“零除”不会在Dart中崩溃?

为什么addTarget不会在Swift中删除?

为什么z不会在最小堆中冒泡?

为什么按钮动画不会在Unity中触发?

为什么这不会在 html 中折叠

为什么我的ibjects不会在javascript中变形?

状态变化不会在道具中传播

为什么模糊事件不会在对 textarea 做出反应时触发?

为什么iPhone的Safari不会在输入type = file上触发更改的事件?

为什么“ drop”事件不会在此网页上触发?

为什么更改输入不会在更改事件上触发?

为什么<Link>和<Redirect>不会在connected-react-router中触发state.router的更新?

Onerror和onload事件不会在React中的脚本元素中触发

使用带有函数参数的 .bind() 时,事件不会在 mousedown 上更新

核心上的事件不会在 cytoscape.js / react 中捕获

为什么我的react变量不会在DOM上呈现?

QtQuick Grid,鼠标事件不会在相邻的鼠标区域上传播

在Rabbitmq中收到消息后,如果出错,为什么消息不会在队列中删除

为什么函数体中的冒号不会在JavaScript中引发错误?