Do I know any Blazor experts here?

If I click the "MyComponent" button below, I get an error: "The render handle is not yet assigned." However, if I first click the "Click me" button, and THEN the "MyComponent" button, it will work as expected.

HOWEVER, if I change the method group invocation in the @onClick handler to a lambda, i.e, "() => _myComponentRef.ShowComponent()" everything will always work.

Does anyone know why this happens?

Blazor code:

@page "/counter"
@rendermode InteractiveServer

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>
<p role="status">Current count: @_currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<button class="btn btn-primary" @onclick="_myComponentRef.ShowComponent">MyComponent</button>

<MyComponent @ref="_myComponentRef"></MyComponent>

@code {
    private int _currentCount = 0;
    private MyComponent _myComponentRef = null!;

    private void IncrementCount()
    {
        _currentCount++;
    }

    protected override void OnInitialized()
    {
        _myComponentRef = new MyComponent();
        base.OnInitialized();
    }
}Blazor code:

@if (Show)
{
    <h3>MyComponent</h3>   
}

@code {
    public bool Show { get; set; }
    
    public void ShowComponent()
    {
        Show = true;
    }
}
0

If you have a fediverse account, you can quote this note from your own instance. Search https://mstdn.social/users/patriksvensson/statuses/115578332983004206 on your instance and quote it. (Note that quoting is not supported in Mastodon.)