JavaScript interop in WebAssembly
All the things mentioned so far in this chapter will work great for Blazor Server and Blazor WebAssembly.
But with Blazor WebAssembly, we have direct access to the JSRuntime
(since all the code is running inside the browser). Direct access will give us a really big performance boost. For most applications, we are doing one or two JavaScript calls. Performance is not really going to be a problem. Some applications are more JavaScript-heavy though and would benefit from using the JSRuntime
directly.
We have had direct access to the JSRuntime
using the IJSInProcessRuntime
and IJSUnmarshalledRuntime
. But with .NET 7, both are now obsolete, and we have gotten a nicer syntax.
In the GitHub repository, I have added a couple of files to the SharedComponents
project if you want to try the code.
We will start by looking at calling JavaScript from .NET. Please note that since our project is prerendering on the server, these code samples will...