Pascal Mathys
Pascal Mathys

Reputation: 609

Initialize context data on Blazor WASM app start

I'm developing a mandate based application where i want to load the "context" data like the mandate name, user information like name, language and other stuff before the user sees the app.

Where would be the place to do this? If i load the data in OnInitializedAsync of App.razor, the data is only present after the app is already visible. If i set the Culture based on the received language, the components are already rendered with the wrong culture. Every tutorial mentions the Program class as the place to set the Culture to be there from the start. Is there a way to make and await this WebApi-call and populate this data at a point where setting the Culture has an impact on the initial render?

Upvotes: 1

Views: 838

Answers (1)

Mayur Ekbote
Mayur Ekbote

Reputation: 2080

Put this in the OnInitializedAsync() method of your MainLayout.razor

Your code can look something like this

MainLayout.razor

<CascadingParameter Value="@mandate">
<div class="p-4">
 @if(loading)
 {
    <p> Loading .... </p>
   
 }else
 {
    @Body
 }
</div>
</CascadingParameter>

@code
{
   Mandate mandate; // Assuming some Mandate class which holds information
   bool loading;
   protected override Task OnInitializedAsync()
   {
     loading = true;
     mandate = await LoadMandate(); /// load preferences from the database etc 
     loading = false;
   }

}

Ideally, push this part to a service and load it there.

Upvotes: 2

Related Questions