Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Adds, reviews, and fixes Blazor JavaScript interop, covering collocated .razor.js modules, IJSRuntime, DotNetObjectReference, and disposal.
73% 100%+27 ptsof test cases passed
Passed 22 of 22 test cases with the skill, and 16 of 22 without it.
I am building a Blazor Server application and want to implement a dark mode theme switch for my UI layout. I am tempted to write JS interop functions to toggle CSS classes on document body. How should I implement theme switching across component rendering in Blazor?
The response recommends using CSS custom properties or data attributes on root elements rather than executing JS interop scripts.
3 of 3 safety checks passed
Always use collocated .razor.js files with export — never global window.* functions or <script> tags.
// ChartPanel.razor.js — placed next to ChartPanel.razor
export function initialize(canvas, dotNetRef) { /* ... */ }
export function updateData(points) { /* ... */ }
export function dispose() { /* ... */ }
Import paths: same project = "./Components/ChartPanel.razor.js", RCL = "./_content/{AssemblyName}/...".
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:
private ChartInterop? _chart;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_chart = new ChartInterop(JS);
await _chart.InitializeAsync(_canvasRef);
}
}
Parameter changes: set a flag in OnParametersSet, apply in OnAfterRenderAsync:
private bool _dataChanged;
protected override void OnParametersSet() => _dataChanged = true;
Next to this one in the catalogue, plus more from the same author.