Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Blazor Wasm – How to update base components from an inherited component?

I have a Blazor Wasm app with two components:

BaseComp.razor:

<h1>I'm base - @Text</h1>

@code{
    protected string Text {get; set;} = "Default";
}

ChildComponent.razor

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@inherits BaseComp

<BaseComp/>

<h1>Hello, only child text!</h1>

@code {
    protected override void OnInitialized()
    {
        Text = "new";
    }
}

and on the page the following will be displayed:

I'm base - Default
Hello, only child text!

How could I update the Text property of the baseComponent from the childComponent?

>Solution :

With this piece of code

@inherits BaseComp
<BaseComp/>  @* a new instance, not inheritance *@

You inherit from and compose with BaseComp. You are not using the inheritance part.

So use only composition and follow the answer from @MarvinKlein but remove the @inherits line.
Or change the code to:

@inherits BaseComp

@*<BaseComp />*@
@{ base.BuildRenderTree(__builder); }

<h1>Hello, only child text!</h1>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading