The sample tries to use JS Interop in the OnInitialized event which will throw. It must be in the OnAfterRender event, like this:
@inject IJSRuntime JS
@if (latency is null)
{
<span>Calculating...</span>
}
else
{
<span>@(latency.Value.TotalMilliseconds)ms</span>
}
@code {
private DateTime startTime;
private TimeSpan? latency;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
startTime = DateTime.UtcNow;
var _ = await JS.InvokeAsync<string>("toString");
latency = DateTime.UtcNow - startTime;
StateHasChanged();
}
}
}
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Thanks @marin-bratanov ... The code originally came over from engineering like that. Later, this was reported, but we missed an update. 🙈 This was asked about on #16615, but Artak closed it without realizing that the reader was working from the published code, and I didn't catch it at the time ...
https://github.com/dotnet/AspNetCore.Docs/issues/16615#issuecomment-580876488
I'll get that fixed today. Thanks for writing in on it.
No worries, I just saw the issue and the "feedback" button and sent it, I didn't even look for previous ones (maybe I should have). Btw, that was probably the most detailed history for such a small thing I've ever seen :) Thank you so much for your efforts :)
You did the right thing. We missed it. When Artak closed it, I'm sure that I was buried in something else and didn't give it a second thought.
We're good now. The update will go live by the end of day. Thanks again.