Skip to content

Blazer: The lifecycle function in the page is activated twice on OnInitializedAsync #14977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AutismPatient opened this issue Oct 14, 2019 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@AutismPatient
Copy link

I want to know why it activated twice to execute the twice load method, which confused me, I was using the official template code!
protected override async Task OnInitializedAsync() { forecasts = await ForecastService.GetForecastAsync(DateTime.Now); }

@Postlagerkarte
Copy link

The behavior you're seeing is by-design. OnInitializedAsync is called once during pre-rendering and once during the rendering.

@AutismPatient
Copy link
Author

I put a load mask on the page so that when I refresh the page, the mask doesn't appear for the first time, the data flashes before it appears, and I'm sure my logic is correct

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Oct 14, 2019
@javiercn
Copy link
Member

@AutismPatient Thanks for contacting us.

@Postlagerkarte answer is correct.

  • Prerendering creates the component and produces an initial render that gets serialized into HTML.
  • After that, the app gets bootstrapped from the client and produces one or more renders in case there's async work:
    • One render after the synchronous work in OnInitializeAsync is done.
    • One render after the asynchronous work in OnInitializeAsync is completed.

That's likely why you see the data flashing. Prerendering waits until the async work is done before producing HTML to produce a meaninful result.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

3 participants