Reputation: 2196
I browse to abc.razor OnInitializedAsync() method gets called.
now in OnvalidSubmit method I have the following code
NavManager.NavigateTo("/abc?rid=1", false, true);
which means it should navigate to abc.razor page again. It does navigate but doesnot call the OnInitializedAsync() method?
why so?
Upvotes: 1
Views: 1470
Reputation: 30545
If you navigate to self, i.e. to the same page, the Router component passes the same route component into RouteView
in App
.
The Renderer sees no component change so treats the event as a re-render. OnInitialized{Async}
is not run.
OnParametersSet{Async}
is run and the Rid
parameter will be updated. So use OnParametersSet{Async}
for your logic.
Here's my Demo Page:
@page "/Reload"
<h3>Reload</h3>
@inject NavigationManager NavManager
<div class="alert alert-info">
@this.Rid
</div>
<div class="alert alert-primary">
@this.value
</div>
<BasicComponent [email protected] />
<div class="m-2">
<button class="btn btn-primary" @onclick=this.ReloadComponent>Reload</button>
</div>
@code {
private string value = string.Empty;
[Parameter]
[SupplyParameterFromQuery]
public string? Rid { get; set; }
private void ReloadComponent()
{
NavManager.NavigateTo($"/Reload?Rid={DateTime.Now.ToLongTimeString()}");
}
protected override void OnParametersSet()
{
value = $"Reloaded at {DateTime.Now.ToLongTimeString()}";
}
}
And BasicComponent.razor:
<h3>BasicComponent</h3>
<div class="alert alert-dark">
@RID
</div>
<div class="alert alert-secondary">
@this.value
</div>
@code {
private string value = string.Empty;
[Parameter] public string RID { get; set; } = string.Empty;
protected override void OnParametersSet()
{
value = $"Reloaded at {DateTime.Now.ToLongTimeString()}";
}
}
The relevant code in ComponentBase
looks like this. _initialized
is already true.
public virtual Task SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);
if (!_initialized)
{
_initialized = true;
return RunInitAndSetParametersAsync();
}
else
{
return CallOnParametersSetAsync();
}
}
Upvotes: 7