BlazorDatasheet icon indicating copy to clipboard operation
BlazorDatasheet copied to clipboard

Issues with Virtualise2D.razor and related JS (sheet-pointer-input, menu-target etc.) causing runtime errors

Open gwwzzf opened this issue 4 months ago • 3 comments

I wrote an issue using ChatGPT.

Description:

While using Virtualise2D.razor with BlazorDatasheet, I encountered multiple runtime errors related to JS event registration and UI virtualization. These issues occur when elements are not fully rendered but JS tries to attach event listeners.

Problems observed:

PointerInputService

Error: Cannot read properties of null (reading 'addEventListener')

Cause: sheetElement is not yet rendered when registerPointerEvents is called.

Effect: Pointer events (pointerup, pointerdown, pointermove, dblclick) fail to register.

MenuTargetService

Error: Cannot read properties of null (reading 'addEventListener')

Cause: The target element is not yet rendered when setContextListener is called.

Virtualise2D.razor

When rapidly changing selections or calling GenerateSheetBySelection, the UI sometimes updates incorrectly.

Using StateHasChanged() followed by Task.Delay() does not reliably wait for the DOM to render before JS initialization.

Expected behavior:

Event registration in JS should wait until the DOM element is available, using either a retry mechanism or lifecycle hooks.

Virtualisation updates in Virtualise2D should correctly reflect the previous selection if actions are throttled.

No runtime errors should be thrown when elements are temporarily null.

When I debug locally, everything works fine. However, after deploying to the server, probably due to network latency, the following error occurs:

Cannot read properties of null (reading 'addEventListener') TypeError: Cannot read properties of null (reading 'addEventListener') at MenuTargetService.setContextListener (http://x.x.x.x:5000/_content/BlazorDatasheet/js/menu-target.js:13:23) at http://x.x.x.x:5000/_framework/blazor.web.js:1:3047 at new Promise () at y.beginInvokeJSFromDotNet (http://x.x.x.x:5000/_framework/blazor.web.js:1:3004) at gn._invokeClientMethod (http://x.x.x.x:5000/_framework/blazor.web.js:1:62730) at gn._processIncomingData (http://x.x.x.x:5000/_framework/blazor.web.js:1:60119) at xn.onreceive (http://x.x.x.x:5000/_framework/blazor.web.js:1:53760) at WebSocket. (http://x.x.x.x:5000/_framework/blazor.web.js:1:81866)

gwwzzf avatar Aug 13 '25 13:08 gwwzzf

The server UI reported the following error: fail: BootstrapBlazor.Components.ErrorLogger[0] BootstrapBlazorErrorBoundary OnErrorAsync log this error Microsoft.JSInterop.JSException: Cannot read properties of null (reading ' addEventListener') TypeError: Cannot read properties of null (reading 'addEventListener') at MenuTargetService.setContextListener https:///_con tent/BlazorDatasheet/js/menu-target.js:13:23) at https:///_framework/blazor.web.js:1:3047 at new Promise () at y.beginInvokeJSFromDotNet https:///_framework/blaz or.web.js:1:3004) at gn._invokeClientMethod https:///_framework/blazor. web.js:1:62730) at gn._processIncomingData https:///_framework/blazor .web.js:1:60119) at xn.onreceive https:///_framework/blazor.web.js:1:5 3760) at WebSocket. https:///_framework/blazor.w eb.js:1:81866) at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstan ceId, String identifier, Object[] args) at Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeVoidAsync(IJSO bjectReference jsObjectReference, String identifier, Object[] args) at BlazorDatasheet.Menu.SheetMenuTarget.OnAfterRenderAsync(Boolean firs tRender) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledT ask(Task taskToHandle, ComponentState owningComponentState)

gwwzzf avatar Aug 13 '25 13:08 gwwzzf

Hi @gwwzzf can you give more information on when this occurs? Is it blazor server or wasm?

anmcgrath avatar Sep 12 '25 02:09 anmcgrath

Title: Error when quickly switching buttons in Blazor Server app (likely due to network latency)

Environment:

Blazor Server

Local demo (no issue)

Server deployment (error occurs when clicking quickly)

Description: I’m using the Blazor Server model. On the web page, I created several buttons, and clicking different buttons generates new calculation tables.

When I demonstrate it locally, I can click the buttons quickly without any errors or messages.

After deploying it on the server:

If I click the buttons slowly → no error.

If I quickly switch between buttons → error occurs, possibly due to network latency.

Steps to Reproduce:

Deploy the app to a remote server.

Open the web page with multiple buttons.

Quickly click and switch between different buttons.

Expected Behavior:

The app should handle quick button clicks gracefully without errors.

Actual Behavior:

Errors occur when quickly switching buttons on the server side.

What I’ve Tried:

Followed AI suggestions and modified Virtualise2D.razor and some .js code.

The error still occurs.

gwwzzf avatar Sep 15 '25 14:09 gwwzzf