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?


🧠 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 AlgoLassi and get an answer plus the tutorials worth studying next.

Ask a question

đŸ’Ŧ Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.