While a Blazor Server app is prerendering, certain actions, such as calling into JavaScript, aren't possible because a connection with the browser hasn't been established. Components may need to render differently when prerendered. To delay JavaScript interop calls until after the connection with the browser is established, you can use the `OnAfterRenderAsync` component lifecycle event. This event is only called after the app is fully rendered and the client connection is established. ```cshtml @using Microsoft.JSInterop @inject IJSRuntime JSRuntime @code { private ElementReference myInput; protected override void OnAfterRender(bool firstRender) { if (firstRender) { JSRuntime.InvokeAsync( "setElementValue", myInput, "Value set after render"); } } } ``` The following component demonstrates how to use JavaScript interop as part of a component's initialization logic in a way that's compatible with prerendering. The component shows that it's possible to trigger a rendering update from inside `OnAfterRenderAsync`. The developer must avoid creating an infinite loop in this scenario. Where `JSRuntime.InvokeAsync` is called, `ElementRef` is only used in `OnAfterRenderAsync` and not in any earlier lifecycle method because there's no JavaScript element until after the component is rendered. `StateHasChanged` is called to rerender the component with the new state obtained from the JavaScript interop call. The code doesn't create an infinite loop because `StateHasChanged` is only called when `infoFromJs` is `null`. ```cshtml @page "/prerendered-interop" @using Microsoft.AspNetCore.Components @using Microsoft.JSInterop @inject IJSRuntime JSRuntime

Get value via JS interop call: @(infoFromJs ?? "No value yet")

Set value via JS interop call:

@code { private string infoFromJs; private ElementReference myElem; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender && infoFromJs == null) { infoFromJs = await JSRuntime.InvokeAsync( "setElementValue", myElem, "Hello from interop call"); StateHasChanged(); } } } ```