❔ Blazor JSInterop declare component-scoped javascript variables

Hi,
I've the following problem: I have a bunch of Blazor components that needs to invoke some javascript methods; each of these components needs to work with its own instance of an object created in the javascript code, let's call it object O.

So I'd need
  • component A to have instance A of object O (let's call it OA)
  • component B to have instance B of object O (let's call it OB)
  • and so on...
I've tried declaring a global variabile in the .js file, but that way there is only one instance across the whole application: I've initialized the global object O in Blazor component A and later I've read its value from Blazor component B, and I've read the value set by A. That seems to confirm that there is a single instance of O across the whole application.

The other way I thought of was to have javascript return to Blazor the object it created. But I think this is not possible since this is not a primitive type, it is a https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection, and I think there is no C# equivalent.

By the way, I need this object because in javascript I open the WebRTC Connection, and later I need to close it, and thus I need a reference to it.

Do you know a way around this?

Thanks,
Alessandro
Was this page helpful?