© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
2 replies
alkasel#159

❔ 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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Cant import scoped Javascript in Blazor Standalone WebAssembly
C#CC# / help
11mo ago
Using JavaScript with Blazor
C#CC# / help
2y ago
✅ Blazor and Javascript loading
C#CC# / help
2y ago
Blazor Date Picker Component
C#CC# / help
2mo ago