State Initialize called twice
See original GitHub issueIt looks like the State is Initialized twice when a BlazorStateComponent is the opening page.
Steps to test with with the Blazor Counter example:
- Put a break-point on the Initialize method of the CounterState class.
- Run the app and select the Counter page. The app will break once on the Initialize method.
- Refresh the page (stay on the Counter page).
- 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:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
If you don’t need prerendering you can change it in /Pages/_Host.cshtml
Replace ServerPrerendered with Server