aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Components without ChildContent dont throw exception when used with contents if shown conditionally

Open floreseken opened this issue 8 years ago • 2 comments

Consider this component:

<div>
    @Test
</div>

@functions
{
    public string Test { get; set; } = "Testing";

}

Works fine if you use it like this somewhere in a page:

<MyComponent></MyComponent>

But if you (accidently) add content to it, like this:

<MyComponent>Do not do this</MyComponent>

It will throw a nice exception, something like: 'Object of type 'Components.MyComponent' does not have a property matching the name 'ChildContent'.

Fine

Now, when I want to show this component conditionally, like so:

@if (show)
{
    <MyComponent>Do not do this</MyComponent>
}

And make a button to set 'show = true' then nothing happens. No component is shown which is maybe to be expected, but there is also no exception, which I do expect.

floreseken avatar Apr 19 '18 14:04 floreseken

I think you might have to call StateHasChanged() when you set show=true

dlr1 avatar Apr 19 '18 14:04 dlr1

Oh wait.. you are right.. My bad.

This StateHasChanged thing keeps biting me..

floreseken avatar Apr 19 '18 15:04 floreseken