JS Interop in Blazor
1. Collocated JS Modules
Always use collocated .razor.js files with export — never global window.* functions or <script> tags.
Import paths: same project = "./Components/ChartPanel.razor.js", RCL = "./_content/{AssemblyName}/...".
2. Lifecycle Timing
All JS interop must happen in OnAfterRenderAsync or event handlers — never in OnInitialized, OnParametersSet, or constructors. JS is not available during server prerendering.
Use a typed interop wrapper (see Section 4) — never call InvokeAsync/InvokeVoidAsync with raw string literals:
Parameter changes: set a flag in OnParametersSet, apply in OnAfterRenderAsync:
3. Batch Related Operations
Each JS interop call crosses the .NET-to-JS boundary (and in Blazor Server, the SignalR circuit). Batching applies in both directions — .NET→JS and JS→.NET.
.NET → JS: merge consecutive calls
If the C# side makes two or more JS calls in a row, combine them into one JS function:
JS → .NET: batch callbacks
When JS needs to send multiple pieces of data back to .NET, send them in a single invokeMethodAsync call rather than making separate callbacks:
Rule: if two interop calls always happen together from either side, merge them into one function.
4. Typed Interop Wrapper
Encapsulate interop for a feature in a plain class that owns the module lifecycle:
The component creates and uses the wrapper with no magic strings:
Prefer a concrete class over interface + implementation for interop wrappers. For unit testing, substitute IJSRuntime directly (it is already an interface).
5. DotNetObjectReference for JS-to-.NET Callbacks
On the JS side, wrap the dotNetRef in a class. Use async/await with try/catch (not .catch()) to guard against circuit loss. Define .NET method name constants at the top:
Rules:
[JSInvokable]methods must bepublic— private/internal silently fails at runtime- Wrap
StateHasChangedinInvokeAsyncinside[JSInvokable]callbacks: - Always
try/catcharoundinvokeMethodAsyncin JS — circuit loss throws - Use
constfor .NET method name strings in JS — prevents typo bugs that silently fail - Dispose
DotNetObjectReferenceinDisposeAsync
6. Disposal and Server Safety
Always implement IAsyncDisposable. Call JS cleanup first, then dispose references. Catch JSDisconnectedException for Blazor Server circuit loss:
Never use sync IDisposable for JS interop cleanup — InvokeVoidAsync returns ValueTask and must be awaited.
7. ElementReference
Pass DOM elements via @ref, not string IDs:
Checklist
- JS is in collocated
.razor.jswithexport— nowindow.*globals - All interop in
OnAfterRenderAsyncor event handlers — never during prerender -
IAsyncDisposablecatchesJSDisconnectedException -
DotNetObjectReferencedisposed inDisposeAsync; JS side hastry/catcharoundinvokeMethodAsync -
[JSInvokable]methods arepublicand useawait InvokeAsync(StateHasChanged) -
InvokeVoidAsyncused when no return value is needed -
ElementReferenceinstead of string IDs - Related operations batched into single interop calls (both .NET→JS and JS→.NET)


