Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

blazor ref to component in if

<button OnClick="Show">Show bla bla bla</button>

@if (IsVisible)
{
  <div @ref="MyRef">Bla bla bla</div>
}

@code {
  private bool IsVisible = false;
  ElementReference MyRef;
  
  public async Task Show()
  {
    IsVisible = true;
    
    // Wait for the Renderer service empty it's queue
    // or in another words: 
    // wait for the non rendered element to apply its reference
    StateHasChanged();
    await Task.Yield();
    
    var anotherRef = MyRef;
  }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #blazor #ref #component
ADD COMMENT
Topic
Name
6+4 =