Skip to content

Commit db45b67

Browse files
authored
Update JSObjectReference to IJSObjectReference (#20069)
1 parent 26b2362 commit db45b67

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

aspnetcore/blazor/call-javascript-from-dotnet.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,13 @@ export function showPrompt(message) {
499499
Add the preceding JavaScript module to a .NET library as a static web asset (`wwwroot/exampleJsInterop.js`) and then import the module into the .NET code using the <xref:Microsoft.JSInterop.IJSRuntime> service. The service is injected as `jsRuntime` (not shown) for the following example:
500500

501501
```csharp
502-
var module = await jsRuntime.InvokeAsync<JSObjectReference>(
502+
var module = await jsRuntime.InvokeAsync<IJSObjectReference>(
503503
"import", "./_content/MyComponents/exampleJsInterop.js");
504504
```
505505

506506
The `import` identifier in the preceding example is a special identifier used specifically for importing a JavaScript module. Specify the module using its stable static web asset path: `_content/{LIBRARY NAME}/{PATH UNDER WWWROOT}`. The placeholder `{LIBRARY NAME}` is the library name. The placeholder `{PATH UNDER WWWROOT}` is the path to the script under `wwwroot`.
507507

508-
<xref:Microsoft.JSInterop.IJSRuntime> imports the module as a `JSObjectReference`, which represents a reference to a JavaScript object from .NET code. Use the `JSObjectReference` to invoke exported JavaScript functions from the module:
508+
<xref:Microsoft.JSInterop.IJSRuntime> imports the module as a `IJSObjectReference`, which represents a reference to a JavaScript object from .NET code. Use the `IJSObjectReference` to invoke exported JavaScript functions from the module:
509509

510510
```csharp
511511
public async ValueTask<string> Prompt(string message)
@@ -514,6 +514,28 @@ public async ValueTask<string> Prompt(string message)
514514
}
515515
```
516516

517+
`IJSInProcessObjectReference` represents a reference to a JavaScript object whose functions can be invoked synchronously.
518+
519+
`IJSUnmarshalledObjectReference` represents a reference to an JavaScript object whose functions can be invoked without the overhead of serializing .NET data. This can be used in Blazor WebAssembly when performance is crucial:
520+
521+
```javascript
522+
window.unmarshalledInstance = {
523+
helloWorld: function (personNamePointer) {
524+
const personName = Blazor.platform.readStringField(value, 0);
525+
return `Hello ${personName}`;
526+
}
527+
};
528+
```
529+
530+
```csharp
531+
var unmarshalledRuntime = (IJSUnmarshalledRuntime)jsRuntime;
532+
var jsUnmarshalledReference = unmarshalledRuntime
533+
.InvokeUnmarshalled<IJSUnmarshalledObjectReference>("unmarshalledInstance");
534+
535+
string helloWorldString = jsUnmarshalledReference.InvokeUnmarshalled<string, string>(
536+
"helloWorld");
537+
```
538+
517539
## Use of JavaScript libraries that render UI (DOM elements)
518540

519541
Sometimes you may wish to use JavaScript libraries that produce visible user interface elements within the browser DOM. At first glance, this might seem difficult because Blazor's diffing system relies on having control over the tree of DOM elements and runs into errors if some external code mutates the DOM tree and invalidates its mechanism for applying diffs. This isn't a Blazor-specific limitation. The same challenge occurs with any diff-based UI framework.

0 commit comments

Comments
 (0)