use-js-interop

Implements and reviews JavaScript interop patterns in Blazor components.

Updated Jul 2, 2026
One-click install
npx skills add https://github.com/ecoDriverltd/FoundryAgentsExperiment --skill use-js-interop-ecodriverltd
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: use-js-interop
Source: https://github.com/ecoDriverltd/FoundryAgentsExperiment/tree/main/.agents/skills/use-js-interop
Command: npx skills add https://github.com/ecoDriverltd/FoundryAgentsExperiment --skill use-js-interop-ecodriverltd

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Blazor developers frequently make JavaScript interop mistakes that cause silent runtime failures, memory leaks, and broken prerendering, such as calling JS during initialization, using global window functions, or leaking DotNetObjectReference instances. ## Core Features & Use Cases - Collocated JS Modules: Enforces .razor.js files with export instead of global window.* functions or inline script tags. - Lifecycle and Disposal Safety: Ensures interop runs only in OnAfterRenderAsync, with IAsyncDisposable cleanup that catches JSDisconnectedException for Blazor Server circuit loss. - Typed Interop Wrappers: Replaces magic-string InvokeAsync calls with strongly typed wrapper classes owning module lifecycle and constants. - Use Case: When adding a chart to a Blazor page, apply this Skill to create a collocated JS module, a typed ChartInterop wrapper, batched update calls, and correct disposal so the component works under server prerendering without leaks. ## Quick Start Add JavaScript interop to my Blazor component following the collocated module and typed wrapper patterns.

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 using IJSRuntime inside OnAfterRenderAsync, never in OnInitialized or constructors, because JS is unavailable during server prerendering. Import a collocated .razor.js module and invoke its exported functions through a typed wrapper class.

How to call .NET methods from JavaScript in Blazor?▼

Pass a DotNetObjectReference to JS and call invokeMethodAsync with the name of a public [JSInvokable] method. Wrap the call in try/catch since circuit loss throws, and use await InvokeAsync around StateHasChanged inside the callback.

Why does my Blazor JS interop fail during prerendering?▼

JS interop fails during prerendering because JavaScript is not available until the component renders on the client. Move all interop calls into OnAfterRenderAsync with a firstRender guard, and apply parameter-driven updates there using a change flag.

Why is my JSInvokable method not being called from JavaScript?▼

JSInvokable methods must be public; private or internal methods silently fail at runtime. Also verify the method name string in JS matches exactly, ideally defined as a const at the top of the module to prevent typos.

How do I dispose JS interop resources in Blazor Server?▼

Implement IAsyncDisposable, call the JS dispose function, then dispose the module and DotNetObjectReference. Wrap the JS calls in try/catch for JSDisconnectedException, since the circuit may already be gone when disposal runs.

When should I avoid JavaScript interop in Blazor?▼

Avoid JS interop when CSS can achieve the same result, such as theming with custom properties, data attributes, or pseudo-classes. Also avoid many fine-grained interop calls; batch related operations into single functions in both directions.