-
I have two components: the child component uses However, using Is Blazor's handling of events similar to React? Child.razor <span style="border: 1px solid red;"
@onclick="OnClick"
@onclick:stopPropagation="@true">
child
</span>
@code {
private void OnClick()
{
Console.WriteLine("child click");
}
} Parent.razor @inject IJSRuntime JsRuntime
<div @ref="@_ref" id="parent">
<Child />
</div>
@code
{
private ElementReference _ref;
protected override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
JsRuntime.InvokeVoidAsync("addClick", _ref);
}
return base.OnAfterRenderAsync(firstRender);
}
}
window.addClick = function (dom) {
dom.addEventListener("click", (e) => {
console.log("on click", dom.getAttribute("id"));
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Blazor relies on event delegation and will listen to the events at the root, which means events bubble all the way before they get stopped. If you want to stop the event at the element level you need to use JS interop to setup a handler on the element itself. |
Beta Was this translation helpful? Give feedback.
Blazor relies on event delegation and will listen to the events at the root, which means events bubble all the way before they get stopped. If you want to stop the event at the element level you need to use JS interop to setup a handler on the element itself.