3
votes

I have 3 components. When I want is to call the New() method of component C from component B. That works with following code (I get the Console.Writeline). But the UI is not updating. I have tried to set StateHaseChanged() in the New() method, but that gave me following errors. Many Thanks!

Error

WASM: System.InvalidOperationException: The render handle is not yet assigned.

ComponentA (SideBar Item)

<NavLink href="@href">
    <div @onclick="OnClick.InvokeAsync">@Text</div>
</NavLink>


@code {
    [Parameter] public string Text { get; set; }
    [Parameter] public EventCallback OnClick { get; set; }
}

ComponentB (SideBar)

<ComponentA Text="Something" Onclick="@(() => componentC.New())" />


@code {
    ComponentC componentC = new ComponentC();
}

ComponentC (Page)

@code {
    public void New()
    {
        //Dostuff..
        Console.WriteLine("Testing");
        //StateHasChanged();

    }
}
1
I assume your component naming is slightly off. Probably ComponentB uses ComponentC internally (in the @code block). Is this correct?Artak
@Artak That was indeed correct, I have changed it in my question. Thanks!Thijs Boudrez

1 Answers

1
votes

Try to use ComponentC in ComponentB as follows:

<ComponentA Text="Something" Onclick="@(() => componentC.New())" />
<ComponentC @ref="componentC" />

@code {
    private ComponentC componentC;
}

The ComponentBase class implements the IComponent.Attach(RenderHandle) method, which is not being called when you simply new-up the component in the code. However, when you do this in the markup, things are wired up correctly.