Blazor – Cascading Parameters with Example

Understanding Cascading Parameters in Blazor

Blazor, part of the ASP.NET Core ecosystem, makes it straightforward to build interactive web applications using C#. One of the key features that enhances component communication is the cascading parameter. This feature enables parent components to hierarchically provide data or services to their descendant components, simplifying state management and data flow across the application.

 

What are Cascading Parameters?

Cascading parameters allow you to pass data or services down through a component tree without explicitly passing them through every level. This is particularly useful for global settings, themes, or services that need to be accessible to many components at different levels of the hierarchy.

 

Let's walk through a practical example to understand how cascading parameters work. Open Microsoft Visual Studio 2022 => Create Blazor Server App project with .Net8.

 

First, define a service that will hold some shared data. For example, let's create a simple AppState service to manage a counter:

 

AppState.cs

public class AppState

{

    public int Counter { get; set; }

 

    public event Action OnChange;

 

    public void IncrementCounter()

    {

        Counter++;

       NotifyStateChanged();

    }

 

    private void NotifyStateChanged() => OnChange?.Invoke();

}

Register the AppState service in the Startup.cs or Program.cs file so that it can be injected into components:

 

Program.cs

var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.RootComponents.Add<App>("#app");

 

builder.Services.AddScoped<AppState>();

 

await builder.Build().RunAsync();

Create a parent component that will provide the cascading parameter to its descendants:

 

ParentComponent.razor

@page "/parent"

@inject AppState appState

 

<CascadingValue Value="appState">

    <h3>Parent Component</h3>

    <p>Current Counter: @appState.Counter</p>

    <button @onclick="appState.IncrementCounter">Increment Counter</button>

    <ChildComponent />

</CascadingValue>

The AppState service is injected using the @inject directive.

 

The CascadingValue component is used to provide the appState as a cascading parameter to all descendant components.

 

Create a child component that consumes the cascading parameter:

 

ChildComponent.razor

@code {

   [CascadingParameter]

    public AppState AppState { get; set; }

}

 

<h3>Child Component</h3>

<p>Current Counter in Child: @AppState.Counter</p>

<button @onclick="AppState.IncrementCounter">Increment Counter in Child</button>

The [CascadingParameter] attribute is used to indicate that AppState should be provided by the nearest cascading parameter provider.

 

Benefits of Cascading Parameters:

 

Simplicity: Cascading parameters simplify the process of passing data down the component hierarchy, avoiding the need to pass parameters through intermediate components.

 

Global Access: They provide a way to share global state or services (like themes or user preferences) across different components.

 

Reusability: They promote component reusability by allowing components to be more self-contained and less dependent on their position in the hierarchy.

 

Cascading parameters in Blazor offer a clean and efficient way to share data and services across components hierarchically. By leveraging this feature, developers can create more maintainable and scalable applications. Whether you're managing application state, user settings, or global services, cascading parameters provide a robust solution to streamline your Blazor applications.