Blazor Component Parameters Explained
Blazor components become reusable when they can receive data from their parent components. The [Parameter] attribute provides the standard way to pass values into a component.
đĻ Create a Parameter
@code {
[Parameter]
public string Title { get; set; } = "";
}
The component can then render the value:
<h2>@Title</h2>
đ¨âđŠâđ§ Parent to Child
A parent component can provide the value:
<MyCard Title="Welcome to Algolassi" />
This keeps the child component reusable because the parent controls the data.
đĸ Typed Parameters
@code {
[Parameter]
public int Count { get; set; }
}
Typed parameters make the component contract clearer and allow the compiler to catch many incorrect usages.
đ Passing Complex Objects
@code {
[Parameter]
public Customer? Customer { get; set; }
}
Components can receive model objects, collections, and other application-specific types.
đĸ EventCallback for Child-to-Parent Communication
When a child needs to notify its parent, EventCallback is commonly used:
@code {
[Parameter]
public EventCallback OnSave { get; set; }
private async Task Save()
{
await OnSave.InvokeAsync();
}
}
đ§ Key Takeaway
Parameters define how a Blazor component receives data, while EventCallback provides a clean way for a child component to notify its parent.
Continue with đĨ Blazor Tutorials.
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.