Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Extend a Blazor component?

I'm currently using MudBlazor and really like it.

However, there are things I find myself repeatedly adding to components like CancellationTokens, are customizing the template myself.

Is there a way to completely inherit/extend an existing component?

One option is to make a new component that has an instance of the component I want to modify, and to add all of the parameters to my component and map them back to the original, but I feel there has got to be a better way.

like image 562
Tom Crosman Avatar asked Jun 13 '26 01:06

Tom Crosman


1 Answers

Attribute splatting is really handy for creating wrappers or higher level abstractions around other components:

<MudTextField Variant="@Variant" @attributes="@OtherAttributes"></MudTextField>

@code {
    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object> OtherAttributes { get; set; }

    [Parameter]
    // can even set your own defaults
    public Variant Variant { get; set; } = "Variant.Filled"
}

You can also create your own base components and use @inherits for shared logic and parameters.

like image 165
Brandon Pugh Avatar answered Jun 15 '26 22:06

Brandon Pugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!