8

I have a Blazor WASM project that has properties that are initially setup in the OnInitializedAsync() method. All works fine, but if I hit the browser refresh button I get 'Object not set' error because all properties are reset and OnInitializedAsync() doesn't seem to run when you hit the browser refresh button. How does one re-initialized properties in this case? Is there a method that I should be using instead of OnInitializedAsync()?

Thanks

7
  • 1
    Why do you think it's not being called on refresh? Mine seems to be...you can add a console log in there to check. Commented Dec 28, 2020 at 18:48
  • Did you ever solve this? Same thing is happening to me now. Got breakpoints all over the place and commented everything out so it's just a bare bones razor page... and still no lifecycle methods are ever called on hard reload of the page. Commented Jan 16, 2021 at 15:58
  • The OnInitializedAsync() apparently is only called when it is FIRST initialized and not called on a Refresh. After a little investigation, the SetParametersAsync() is always called, so that is probably the function to use in these cases, though not sure if there are any unintended circumstances using that. Commented Jan 22, 2021 at 0:26
  • I added a console write on all for functions: OnInitializedAsync, SetParametersAsync, OnParametersSetAsync and OnAfterRenderAsync. SetParametersAsync() was the only one the ran reliably every time in refreshes and navigating too multiple times. Commented Jan 22, 2021 at 0:29
  • I have a page L for a list of items and LA that's bound to a nested route, like "/items/edit/id-1" to edit the item. The lifecycle events of page LA are not triggered when I refresh the browser, but they are when I navigate from the list L to LA. I'm like o.O wtf is this, and are we supposed to build web apps with this crap?! :) Commented Mar 9, 2021 at 16:59

4 Answers 4

1

I had a similar problem and using the lifecycle event OnParametersSet{Async} worked.

protected override void OnParametersSet() { } 

Or

protected override async Task OnParametersSetAsync() { await ... } 

https://learn.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-5.0#after-parameters-are-set-onparameterssetasync

For some reason break points where not being hit when using the refresh button but I could "trick" the system by using NavigationManager.NavigateTo("/mypath/" + id); instead of refreshing via browser and I could then debug everything that happened.

Useful link for routing:

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-5.0

Sign up to request clarification or add additional context in comments.

Comments

1

I think there is a misunderstanding/miscommunication between the OP and the replies thus far. Here's how it is:

Every Blazor page or component has its own OnInitializedAsync (and AfterRenderAsync etc) If you don't explicitly override them they inherit them from the base class.

These methods are called every time the page loads even after an F5 refresh.

But here's where I believe the misunderstanding lies: Take for example you have a site with two routes. the Index.razor page that's at the '/' route and MyPage.razor page that's at the '/mypage' route

If you navigate to /mypage and then press F5 the OnInitializedAsync method of the Index.razor file will not be hit. Only the one in MyPage.razor will be. If you have important values that are initialized in Index.razor that other pages depend on there will be a problem when you refresh one of those pages.

Comments

0

You can create a component and then cascade it down from app.razor or _host.razor.

Example inside component

<CascadingValue Value="this"> @ChildContent </CascadingValue> @code { [Parameter] public RenderFragment ChildContent { get; set; } protected override void OnInitialized() => Expression.Empty() } 

Comments

-4

The OnInitializedAsync() lifecycle function gets called every time the browser page is refreshed or a component is going to be rendered for the first time.

1 Comment

The question is saying quite clearly that this is not happening for some reason.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.