Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 41 ยท Data Binding and Events

Event Callbacks and Component Communication

Learn how Blazor child components talk to parents with EventCallback and EventCallback of T, why it beats Action and Func, how to build reusable buttons and list items, and how to avoid prop drilling.

Data flows down the component tree through parameters. But how does a child tell its parent that something happened: a button was clicked, an item was chosen, a form was saved? In Blazor the answer is the EventCallback. It is the standard way to let a parent pass a handler to a child, and it behaves better than a plain C# delegate in several important ways.

Declaring and raising a callback

A child declares a parameter of type EventCallback (no data) or EventCallback<T> (with data) and invokes it when something happens:

HTML
@* Components/TodoItem.razor *@
<li>
    <input type="checkbox" checked="@Item.Done" @onchange="Toggle" />
    <span>@Item.Title</span>
    <button @onclick="() => OnDelete.InvokeAsync(Item)">Delete</button>
</li>

@code {
    [Parameter, EditorRequired] public TodoModel Item { get; set; } = default!;
    [Parameter] public EventCallback<TodoModel> OnDelete { get; set; }
    [Parameter] public EventCallback<bool> OnToggled { get; set; }

    private Task Toggle(ChangeEventArgs e) => OnToggled.InvokeAsync((bool)e.Value!);
}

The parent supplies handlers:

HTML
@foreach (var todo in todos)
{
    <TodoItem Item="todo"
              OnDelete="Remove"
              OnToggled="done => Console.WriteLine($"{todo.Title} -> {done}")" />
}

@code {
    private List<TodoModel> todos = new();

    private void Remove(TodoModel item) => todos.Remove(item);
}

When the user clicks Delete, the child calls OnDelete.InvokeAsync(Item). That calls Remove in the parent, which changes the parent's list. The parent renders again and the item disappears. The child never touched the list: data flows down, events flow up.

Why not just use Action or Func?

You can declare [Parameter] public Action<TodoModel> OnDelete { get; set; }, but EventCallback is better:

  • Automatic re-render. When an EventCallback runs, Blazor renders the component that owns the handler (the parent) automatically. With a raw Action, you must call StateHasChanged yourself.
  • Async-aware. InvokeAsync works with both sync and async handlers and awaits the async ones.
  • Safe when unassigned. If the parent did not provide a handler, InvokeAsync simply does nothing. A null Action throws unless you write ?.Invoke.
  • Efficient. EventCallback is a struct and Blazor avoids re-rendering children just because a callback was passed (when the handler is a method group).

Rule of thumb: parameters that receive handlers should be EventCallback.

Passing data with EventCallback of T

EventCallback<T> sends a value to the parent. You pick T: the item, an id, a bool, or a small record for several values.

HTML
<ProductPicker OnPicked="HandlePick" />

@code {
    private void HandlePick(Product p) => selected = p;
}

The handler can return void or Task. You can also use a lambda: OnPicked="p => selected = p".

The Changed convention and binding

A callback named Changed next to a parameter enables @bind-Name, as in Two-Way Data Binding. For custom inputs this is the recommended design:

HTML
<StarRating @bind-Value="rating" />

The child updates the value by calling ValueChanged.InvokeAsync(newValue), and the parent stores it.

Staying simple: avoid long chains

When a deeply nested component needs to notify something far up, passing callbacks through every layer ("prop drilling") gets tedious. Options:

  1. Cascading values (chapter 6) let descendants receive a shared object.
  2. A shared service with an event, injected where needed (chapter 6).
  3. Restructure so the component that owns the data is closer to the component that changes it.

Callbacks remain ideal for the direct parent-child relationship.

Runnable stand-in: EventCallback from scratch

The stand-in below models EventCallback<T> as a tiny struct: it wraps a handler, does nothing when empty, supports sync and async handlers and tells its owner to re-render afterwards. A parent holds a todo list; the child fires callbacks.

C# Stand-in: EventCallback of T

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The child never touches the list, yet the list changes and the parent re-renders: that is the whole contract of EventCallback.

Good practice

  • Name callbacks starting with On: OnSave, OnDelete, OnSelected.
  • Prefer one meaningful payload (an item or a small record) over several loose arguments.
  • Check HasDelegate if you need to know whether a parent subscribed: if (OnClose.HasDelegate) ....
  • Do not mutate parameters in the child; ask the parent through a callback.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a child component notify its parent that a button was clicked?

    The parent passes a handler as an EventCallback; the child invokes it.
  2. 2. What is a key advantage of EventCallback over Action?

    EventCallback triggers the owner's render and silently does nothing when empty.
  3. 3. Which signature enables @bind-Value on a component?

    The {Parameter}Changed naming convention is what @bind-{Parameter} looks for.
  4. 4. What does EventCallback.HasDelegate tell you?

    It is true when a handler has been assigned.

Summary

  • Data flows down through parameters; events flow up through EventCallback and EventCallback<T>.
  • EventCallback re-renders the handler's owner, awaits async handlers and is safe when unset.
  • Changed callbacks enable @bind-Name for custom components.
  • Avoid long callback chains: use cascading values or shared services instead.

Next: Component References and StateHasChanged