I want get data from db once on OnInitializedAsync
. I try to use tableLoading
to judue,but it's not work.
protected override async Task OnInitializedAsync()
{
if (tableLoading)
{
return;
}
tableLoading = true;
users = await userService.GetSome(1, userType);
_total = await userService.GetCount(userType);
tableLoading = false;
Console.WriteLine("OnInitializedAsync");
}
CodePudding user response:
How i know blazor OnInitializedAsync exec in once or twice?
It usually loads twice.
1. Once when the component is initially rendered statically as part of the page.
2. A second time when the browser renders the component.
However, If you want to load it once, in that case, you could go to _Host.cshtml
and change render-mode="ServerPrerendered"
to render-mode="Server"
, and it would be called only once as a result it would then load your data from the database once only.
Note: For more information you could refer to the official documents here
CodePudding user response:
I know it's usually loads twice, i want to know when the function is run, how to konw it's run on once or twice. This is my solution.
static bool first = true;
protected override async Task OnInitializedAsync()
{
if (first)
{
first = false;
Console.WriteLine("first time");
return;
}
Console.WriteLine("second time");
}