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

Blazored LocalStorage State with MudBlazor initialization / rendering to late

I have a switch that selection is stored as a state. The last state is to be taken out of the state when loading the view. That works.

However, you can see the change of state during rendering. Actually, the view should only be visible when the rendering is finished and you can’t see the transition.

Index.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

@page "/"

<MudToolBar>
    <MudSwitch T="bool" Checked="Show" CheckedChanged="HandleShowChanged" />
</MudToolBar>

@if (Show)
{
    <h1>Show something</h1>
}
else
{
    <h1>Nothing to show</h1>
}

Index.razor.cs:

public partial class Index
{
    private bool Show { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Show = await LocalStorage.GetItemAsync<bool>(nameof(Show));
    }

    private async Task HandleShowChanged(bool value)
    {
        Show = value;
        await LocalStorage.SetItemAsync(nameof(Show), value);
    }
}

enter image description here

>Solution :

Your problem is that the rendering happens too soon, and that you can’t prevent. But you can eliminate the relevant parts until loading is finished:

@page "/"

@if (isLoaded)
{
  <MudToolBar>
    <MudSwitch T="bool" Checked="Show" CheckedChanged="HandleShowChanged" />
  </MudToolBar>

  @if (Show)
  {
      <h1>Show something</h1>
  }
  else
  {
     <h1>Nothing to show</h1>
  }
}

I assume you can handle the isLoaded implementation.

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