chanan / BlazorStrap

Bootstrap 4 Components for Blazor Framework
https://blazorstrap.io
The Unlicense
916 stars 157 forks source link

BSDropdown does not unregister document click event properly #528

Closed uecasm closed 2 years ago

uecasm commented 2 years ago
  1. Have a BSDropdown in a form with conditional rendering (e.g. wrapped in an @if linked to a checkbox).
  2. Open the dropdown at least once.
  3. Cause the dropdown to be disposed (e.g. uncheck the checkbox).
  4. Click anywhere on the page.
  5. In the JS Console, the following error is logged:

    Uncaught (in promise) Error: System.ArgumentException: There is no tracked object with id '7'. Perhaps the DotNetObjectReference instance was already disposed. (Parameter 'dotNetObjectId')

It's looking like BSDropdown.ShowActionsAsync is registering a document click event using the dropdown's reference, and then the DisposeAsync is attempting to remove it, although it appears that this is failing to actually do so. I have not investigated why.

I have confirmed that the error is not logged if I set AllowOutsideClick="true", which avoids registering the event in the first place.

I've also confirmed that InteropEventCallback still gets called after opening and closing the dropdown and then clicking on the background, despite the attempt to unregister it in HideActionsAsync.

(Side note: the dispose is also doing things in the wrong order -- it should unregister all events that might possibly call it before disposing the object reference, although that is not the actual problem here.)