I have the following code in my MainLayout.razor. It causes the theme from the cookie to be loaded. At the start though the default is loaded and shortly after that this code is executed causing a flicker for the theme. How can I solve that?
@code {
private bool _initialized;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && !_initialized)
{
_initialized = true; // Prevents multiple calls
// Haal het thema op uit localStorage
var theme = await JSRuntime.InvokeAsync<string>("localStorage.getItem", "SessyTheme");
if (!string.IsNullOrEmpty(theme))
{
NewTheme = theme;
// Pas het thema toe
ThemeService.SetTheme(theme, true);
}
}
}
}