[英]How to make React listen to event in Child component first instead of Parent component?
Consider this example考虑这个例子
<div onClick={() => redirectTo('/foo')}>
<input type="text"/>
<input type="button" onClick={() => redirectTo('/bar')} />
</div>;
when i test this example in this case by click to second input , React always redirect to /foo, is that normal behavior and how can i make React redirect to /bar?在这种情况下,当我通过单击第二个输入来测试此示例时,React 总是重定向到 /foo,这是正常行为吗,我该如何让 React 重定向到 /bar? Thanks a lot!
非常感谢!
The click event is propagating from the input to the div and both redirects occur.点击事件从输入传播到 div 并且两个重定向都会发生。 You are seeing the result of the last redirect.
您正在看到上次重定向的结果。
You can stop the propagation of the input's click event.您可以停止输入的点击事件的传播。
<div onClick={() => redirectTo('/foo')}>
<input type="text"/>
<input
type="button"
onClick={(e) => {
e.stopPropagation();
redirectTo('/bar');
}}
/>
</div>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.