Parameters In Blazor Components (Deep Dive)

Introduction

 
A Blazor Component can exist as either a standalone component, or an entire page with its own route. When annotating properties in a Blazor Component with [Parameter], it allows us to handle incoming parameters with ease. For instance, when we use the [Parameter] attribute, we can accept custom parameters in our component like this:
  1. <MyComponent MyCustomParameter=”value” />  
Or like this, 
  1. https://<host>/MyComponent/value  
The Anatomy of a Blazor Component
 
When we look at the source code for a Blazor Component, we can immediately tell whether or not the component was intended to be used as a standalone page. If it is, then we know right away the pattern for the URL, so we can access this component at run-time. 
  1. @page “/myroute”  
As previously mentioned, the @page directive can also be used to specify the pattern for accepting parameters in the route,
  1. @page “/counter/{MyParameter}”  
  2.   
  3. @code {  
  4.     [Parameter]  
  5.     public string MyParameter { getset; }  
  6. }  
Because of the use of the [Parameter] attribute here, Blazor will automatically initialize the property MyParameter with the corresponding value from the route parameters. For instance, if we run our app and navigate to,
 
/counter/myValue”
 
The value of MyParameter will be the string “myValue” when the component has finished loading. Here is a way we can visualize a simple Parameter,
 
 

A Quick Primer On Attributes in C#

 
If you’re unfamiliar with attributes in C#, it will be hard to understand how this [Parameter] functionality actually works in Blazor. In a previous section, I stated “Blazor will automatically initialize the property …” and this is true. Attributes do not have inherent behaviors in C# or .NET, and they generally do not influence any external data on their own. Attributes are just metadata on a property, and this metadata needs to be consumed by some other code.
 
But how are attributes implemented?
 
Attributes are simple classes that follow a particular naming convention, ClassNameAttribute —signaling to the C# compiler that this class will be used as an annotation in another class.
 
Say, for example, I want a custom attribute called [ValidationApplied]. I would create a new class called ValidationAppliedAttribute which willinherit from Attribute,
  1. class ValidationAppliedAttribute : Attribute  
  2. {  
  3. }  
Now this class can be used as an attribute in another class.
  1. class SomeClass  
  2. {  
  3.     [ValidationApplied] // custom attribute  
  4.     public string MyProperty { getset; }  
  5. }  
What’s interesting about this is that on its own, the attribute doesn’t do anything. It is merely metadata on a property, and doesn’t encapsulate any behaviors. Attributes can have their own properties, too.
  1. class ValidationAppliedAttribute : Attribute  
  2. {  
  3.     public string MyProperty { getset; }  
  4. }  
And now the following syntax is possible:
  1. [ValidationApplied(MyProperty=”some value”)]  
  2. public string SomeProperty { getset; }  
Given the fact that attributes don’t inherently encapsulate any behaviors, we need to write the behaviors ourselves. This is done using reflection elsewhere in the codebase:
  1. var attributes = Attribute.GetCustomAttributes(myObject.GetType()).Dump();  
  2.   
  3. foreach(var attribute in attributes)  
  4. {  
  5.     if(attribute is ValidationAppliedAttribute)  
  6.     {  
  7.         // property has [ValidationApplied]. test validation on property  
  8.     }  
  9. }  
We can be certain this type of reflection is being performed when we use the [Parameter] annotation in a Blazor Component. Using reflection does carry some performance overhead, but over time this overhead has gotten smaller and smaller. I'm only mentioning all of this because it’s a good idea, in my opinion, to be aware of what our code is actually doing when we use these very abstract frameworks—especially when we’re paying for compute time on the Cloud. But, we also need not jump to any conclusions about performance just by looking at an implementation detail alone. For instance, many frameworks that do this type of reflection may implement caching. They’ll do the reflection work once and cache the results. After all, attributes are applied to Types, not Instances. Therefore, the work done using reflection only really needs to be done once per type.
 
Note
I’m not certain whether or not Blazor implements this caching functionality. If anyone has knowledge about this, feel free to share in the comments and I’ll update this article.
 

Optional Parameters

 
In C#, we can create optional values using nullable types. Nullable types are a first-class citizen in Blazor, which means we can take advantage of this syntax to allow for optional parameters in our Blazor Components. Here’s how we can specify an optional parameter in a component,
  1. @page “/counter/{InitialValue:int?}”  
  2.   
  3. @code {  
  4.     private int _count = 0;  
  5.       
  6.     [Parameter]  
  7.     public int? InitialValue { getset; }  
  8.   
  9.     protected override void OnParametersSet()  
  10.     {  
  11.         if (StartValue.HasValue)  
  12.         {  
  13.             _count = InitialValue.Value;  
  14.         }  
  15.     }  
  16. }  
In the @page directive at the top of the component, we’re using a type annotation to tell Blazor this will be an optional int. Below that in the @code section, we are creating a corresponding nullable int property called InitialValue. Once the parameters are set during initialization, we are then checking to see if a value has been provided. If so, we update our count value with InitialValue.
 
Consequently, because Blazor Components don’t care how they are incorporated into your project, all of this logic will work when our component is used as a standalone component in another page! This means we can set InitialValue like this,
  1. /counter/4  
or like this,
  1. <Counter InitialValue=”4” />  
Because as previously mentioned, Blazor Components don’t care whether they’re a page, a component in another page, or a component in another component.
 

Two-Way Data Binding

 
Two-Way Data Binding is a powerful mechanism that allows us to bind to data inside the component (from outside the component,) sending and receiving changes to the component state in real-time. In other words, a parent component can bind a variable to a Parameter within our child component—Any changes made to that parent’s variable will update the bound Parameter in the child component. Likewise, any changes made to the child Parameter will also change the bound variable within the parent component.
 
This is why it’s known as two-way binding. Luckily, adding the capability of two-way binding in a Blazor Component is easy!
 
There are only a couple of additions we need to make to our code in order to enable two-way binding. First, we need to add a corresponding EventCallback for the parameter in question. Take notice of the name given to this EventCallback in the following image; this naming convention is required for Blazor to wire everything up correctly.
 
 
When using this component in a page, we can bind to the parameter via the @bind- attribute. Now, any changes to the bound data will propagate automatically. There is one thing missing in that above example, however. If we change the value of MyParameter from within MyComponent, we need to actually invoke MyParameterChanged to ensure the proper events are fired.
  1. private async Task ChangeValueAsync()  
  2. {  
  3.     MyParameter = "greetings, world!";  
  4.     await MyParameterChanged.InvokeAsync(MyParameter);  
  5. }  
Example: Two-Way Data Binding
 
For this example we’re going to create a component called TwoWayBinding.razor. This is a basic counter component with two-way binding enabled, 
  1. <h3>Value: @Value</h3>  
  2.   
  3. <button class="btn btn-success" @onclick="ChangeValue">Increment Value</button>  
  4.   
  5. @code {  
  6.     [Parameter]  
  7.     public int Value { getset; }  
  8.   
  9.     [Parameter]  
  10.     public EventCallback<int> ValueChanged { getset; }  
  11.   
  12.     private async Task ChangeValue()  
  13.     {  
  14.         ++Value;  
  15.         await ValueChanged.InvokeAsync(Value);  
  16.     }  
  17. }  
Next, in Index.razor we can add this code,
  1. <p>Data From Page: @count</p>  
  2.   
  3. <TwoWayBinding @bind-Value="count"/>  
  4.   
  5. <input type="number" @bind-value="count"/>  
  6.   
  7. @code {  
  8.   
  9.     private int count;  
  10.   
  11. }  
Running this example will yield several text displays and controls, all bound to the same data. Any changes made will automatically propagate to every other control that binds to the data.

 

Conclusion

 
In this article we took a deeper look at what Parameters are in Blazor, how they work, how we can use them, and finally how we can add Two-Way Binding capabilities to a Parameter. I hope after reading this material you’re able to fully understand and use these powerful mechanisms given to us by ASP.NET Core and Blazor.
 
Happy Coding! Stay safe everyone.