how do i use cascading parameters in blazor? in csharp

Cascading parameters in Blazor are used to pass data from a component to all of its descendants in the component tree. To use cascading parameters in Blazor, you need to follow these steps:

  1. Define a cascading parameter in the parent component:
main.cs
[CascadingParameter]
public string ExampleParameter { get; set; }
66 chars
3 lines
  1. Provide the value of the cascading parameter in the parent component:
main.cs
<ParentComponent>
    <ChildComponent />
</ParentComponent>

@code {
    private string exampleValue = "Hello from parent";

    protected override void OnInitialized()
    {
        ExampleParameter = exampleValue;
    }
}
224 chars
13 lines
  1. Access the cascading parameter value in the child component:
main.cs
@code {
    [CascadingParameter]
    public string ExampleParameter { get; set; }
    
    protected override void OnInitialized()
    {
        Console.WriteLine($"Received: {ExampleParameter}");
    }
}
205 chars
10 lines

In this example, the value of ExampleParameter passed from the parent component will be accessible in the child component due to the cascading behavior.

Remember to import the Microsoft.AspNetCore.Components namespace to use [CascadingParameter] attribute.

This is how you can use cascading parameters in Blazor using C#.

related categories

gistlibby LogSnag