question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State Initialize called twice

See original GitHub issue

It looks like the State is Initialized twice when a BlazorStateComponent is the opening page.

Steps to test with with the Blazor Counter example:

  1. Put a break-point on the Initialize method of the CounterState class.
  2. Run the app and select the Counter page. The app will break once on the Initialize method.
  3. Refresh the page (stay on the Counter page).
  4. The app will break twice on the Initialize method.

My workaround is a Loaded logic on the MainLayout.razor component:

    bool Loaded { get; set; }
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            Loaded = true;
            StateHasChanged();
        }
        base.OnAfterRender(firstRender);
    }

No BlazorStateComponents are loaded until the Loaded flag is true.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Herdubreidcommented, Nov 8, 2019

The downside of replacing ServerPrerender with Server is the blank page until it’s rendered – this might only be milliseconds but enough to strike you with the thought of something’s wrong .

My workaround shows the loading icon, which could be expanded to a splash page.

1reaction
mrpmorriscommented, Nov 8, 2019

If you don’t need prerendering you can change it in /Pages/_Host.cshtml

Replace ServerPrerendered with Server

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reack hook useState (initializer function) called twice ...
This is caused by Strict Mode which is wrapping your App . Under Strict mode react intentionally runs some lifecycle methods twice to...
Read more >
In StrictMode, the useState() initializer function is called ...
State initializer is part of render, and should be pure. Running it twice (and ignoring the result of one of the runs) should...
Read more >
StateObject wrongly initialized twice?
Hi I've recently encountered a weird instantiation behaviour where the wrapped value of a @StateObject could be created twice.
Read more >
UseEffect called twice in React 18 - How to fix it? - YouTube
UseEffect called twice in React 18 - How to fix it? In the strict mode of React 18 an effect with useEffect seems...
Read more >
Lifecycle callback initialize called twice when DOM ...
The docs states that the initialize method is called once in init(i supposed once when the page was loaded), whereas the connect is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found