Blazor EventCallback: Passing Events from Child to Parent
Blazor components often need to notify a parent component after a user action. EventCallback provides the standard component pattern for doing this.
đļ Child Component
<button @onclick="NotifyParent">
Save
</button>
@code {
[Parameter]
public EventCallback OnSaved { get; set; }
private async Task NotifyParent()
{
await OnSaved.InvokeAsync();
}
}
đ¨âđŠâđ§ Parent Component
<ChildComponent OnSaved="HandleSaved" />
@code {
private void HandleSaved()
{
// Refresh data or update state.
}
}
đĻ Passing a Value
Use EventCallback<T> when the child needs to send data.
[Parameter]
public EventCallback<int> ItemSelected { get; set; }
await ItemSelected.InvokeAsync(42);
⨠Why EventCallback?
- Works naturally with Blazor component parameters.
- Supports asynchronous callbacks.
- Keeps child components reusable.
- Makes parent-child communication explicit.
đ§ Key Takeaway
Use parameters to pass data down and EventCallback to notify the parent or send data back up.
Explore more at đĨ Blazor Tutorials.
đ¤ AlgoLassi Assistant
Have a question about this tutorial?
Ask a question
Ask AlgoLassi and get an answer plus the tutorials worth studying next.
đŦ Comments
Sign in with Google to publish immediately, or comment anonymously and wait for approval.
Comments will appear here when available.