use-js-interop

Implements and reviews JavaScript interop patterns in Blazor components using collocated modules and typed wrappers.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/D1ssolve/craft-agents --skill use-js-interop-d1ssolve
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: use-js-interop
Source: https://github.com/D1ssolve/craft-agents/tree/main/skills/use-js-interop
Command: npx skills add https://github.com/D1ssolve/craft-agents --skill use-js-interop-d1ssolve

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Blazor developers frequently misuse JavaScript interop by calling JS during prerendering, leaking DotNetObjectReference instances, scattering magic strings, and making excessive round-trips across the .NET-to-JS boundary. This Skill provides the correct patterns for adding, reviewing, or fixing JS interop in Blazor components. ## Core Features & Use Cases - Collocated JS Modules: Enforces .razor.js files with export instead of global window.* functions or <script> tags. - Lifecycle & Disposal Safety: Ensures interop runs only in OnAfterRenderAsync or event handlers, with IAsyncDisposable cleanup that catches JSDisconnectedException. - Typed Interop Wrappers: Encapsulates IJSRuntime calls in plain classes with internal const method names, eliminating magic strings. - Use Case: When building a Blazor chart component that calls a JavaScript charting library, use this Skill to structure the module import, batch interop calls, wire up DotNetObjectReference callbacks, and dispose everything correctly on Blazor Server. ## Quick Start Review my Blazor component's JavaScript interop code and fix any lifecycle, disposal, or batching issues.

Frequently Asked Questions about use-js-interop

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call JavaScript from a Blazor component?

Call JavaScript from Blazor by injecting IJSRuntime and invoking functions in OnAfterRenderAsync or event handlers, never in OnInitialized. Import a collocated .razor.js module with export functions rather than using global window functions.

How to call .NET methods from JavaScript in Blazor?

Create a DotNetObjectReference wrapping your component and pass it to JS, then call invokeMethodAsync with the method name. The target method must be public and decorated with [JSInvokable], and you should wrap StateHasChanged in InvokeAsync.

Why does Blazor JS interop fail during prerendering?

JS interop fails during prerendering because JavaScript is not available until the component renders in the browser. Move all interop calls to OnAfterRenderAsync with a firstRender check, or into event handlers.

How do I dispose JS module references in Blazor Server?

Implement IAsyncDisposable, call the JS dispose function, then dispose the IJSObjectReference and DotNetObjectReference. Wrap the cleanup in try/catch for JSDisconnectedException, since the SignalR circuit may already be gone.

When should I not use JavaScript interop in Blazor?

Avoid JS interop when CSS can achieve the same result, such as theming with custom properties or data attributes. Also avoid it for general component authoring without JS needs, and batch calls when multiple interop operations always occur together.