✅ Similar to Blazor WebAssembly
Parameter
is a feature for transmitting data from a parent component to its direct child components.
You can download the example code used in this topic on GitHub.
Parameter
The following steps will guide you on how to pass parameters from a parent component to a child component in Blazor:
[EditorRequired]
attribute to mark a parameter as required. Additionally, you can set default values for the parameter by defining it in the getter and setter.@code { [Parameter] public string InputParameter { get; set; } = "Default Value"; [EditorRequired] [Parameter] public string RequiredInputParameter { get; set; } = ""; }
@code { public string FirstData { get; set; } = "First data"; public string SecondData { get; set; } = "Second data"; }
[EditorRequired]
attribute, it must be passed, otherwise, you will receive a warning that there is a required parameter.<Child InputParameter="FirstData" RequiredInputParameter="SecondData" />
You have learned that in order to pass data from a parent component to a child component, the child component must first acknowledge the parameters that it expects to receive. However, in some cases, it may not be possible to predict which parameters will be passed to the component. A common scenario that demonstrates this situation is when working with HTML attributes (learn more at https://www.w3schools.com/html/html_attributes.asp). As you know, any HTML attribute can be defined and as a result, the child component will not know which or what attributes the parent component will pass. In such cases, Blazor's Parameter feature supports the child component by allowing it to catch all unmatched parameter values into a dictionary.
@code { [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> AdditionalAttributes { get; set; } = new(); }
<Child myCustomAttribute myCustomAttribute2="Blazor School"/>
Then your property AdditionalAttributes
in the child component will have all the unmatched parameter values, where you can access them via dictionary keys:
Key | Value |
myCustomAttribute | true |
myCustomAttribute2 | Blazor School |
When utilizing the parameter approach in Blazor, it is crucial to avoid altering the reference of a passed parameter within any child component. The following code sample illustrates this mistake:
@code { [Parameter] public ExampleClass ExampleInstance { get; set; } = new(); public void Correct() { ExampleInstance.Data = "Value changed"; } public void Mistake() { ExampleInstance = new() { Data = "Reference changed" }; } }
Changing the reference of a passed parameter can result in unexpected behaviour, as the change will only be reflected within the child component and not the parent component. This can be demonstrated in the following video:
In Blazor, you can update the value of a parameter in both the parent component and its child components. In this section, you will learn how to:
This allows you to create more dynamic and interactive user interfaces, by keeping the state of your components updated and synchronized.
In Blazor, whenever the parent component updates the value of a passed parameter, the child components will automatically re-render with the new value. In most cases, there is no need to specifically detect these changes as the re-rendering will take care of it.
However, in certain scenarios, you may want to detect changes made to a parameter in the parent component and perform additional actions. In such cases, you can override the OnParametersSet()
method in your child component. This method is called whenever the component receives new parameter values and it gives you the opportunity to perform custom logic. You can find more information about this method and other lifecycle events in the Blazor Component lifecycle tutorial.
EventCallBack
Unlike changes made in the parent component, updates to a passed parameter made by a child component will not automatically trigger a re-render of the parent component. To make the parent component react to these changes, you must pass a delegate to the child component. This delegate will be called by the child component whenever it updates the parameter value, notifying the parent component to update itself.
Here's how to implement this:
@code { [Parameter] public ExampleClass ExampleInstance { get; set; } = new(); [Parameter] public EventCallback ExampleInstanceChanged { get; set; } }
@code { ... public void UpdateValue() { ExampleInstance.Data = "Value changed at child"; ExampleInstanceChanged.InvokeAsync(); } }
StateHasChanged()
along with the parameter to the child component. This will tell the parent component to re-render itself, whenever the child component update the parameter value.<Child ExampleInstance="ExampleInstance" ExampleInstanceChanged="StateHasChanged" />
@bind
You can achieve the same result as EventCallBack
with @bind
. This helps reduce the amount of code you need to write and makes your component easier to understand.
EventCallback<{Type of parameter}>
with the name {Parameter}Changed
.@code { [Parameter] public string? Input { get; set; } [Parameter] public EventCallback<string> InputChanged { get; set; } }
EventCallback
to update it rather than updating the parameter directly. For example:@code { ... public void UpdateInputFromChild() { InputChanged.InvokeAsync("Value changed at child"); } }
@bind-{Parameter}
to pass the parameter to the child component. For example:<Child @bind-Input="PassingInput" /> @code { public string PassingInput { get; set; } = "Data from parent"; }