Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor wasm: Reference Javascript function with IJSObjectReference #38517

Closed
Socolin opened this issue Nov 19, 2021 · 3 comments
Closed

Blazor wasm: Reference Javascript function with IJSObjectReference #38517

Socolin opened this issue Nov 19, 2021 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-jsinterop This issue is related to JSInterop in Blazor ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@Socolin
Copy link

Socolin commented Nov 19, 2021

Describe the bug

When I'm trying to reference a javascript function in JS I'm getting this error.

Uncaught (in promise) Error: Cannot create a JSObjectReference from the value 'function () {
        console.log('Hello !');
}'.

To Reproduce

In a .js file put

function funcReference() {
    return function () {
        console.log('Hello !');
    }
}

Then

@inject IJSRuntime _jsRuntime
...
await _jsRuntime.InvokeVoidAsync("loadScript", "js/launcher-wrapper.js");

Further technical details

  • Version: 6.0

Use case:

What I'm trying to achieve is to call a JS function from .NET and give it a callback to a .NET method.
Something like:

function __simple_callback(name, dotnetReference) {
    return function () {
        console.log('in __simple_callback');
        dotnetReference.invokeMethodAsync(name, ...arguments);
    }
}
await _jsRuntime.InvokeVoidAsync("someJsMethod", DotNetObjectReference.Create(new {
    onError = await _jsRuntime.InvokeAsync<IJSObjectReference>("__simple_callback", "OnError")
});


[JSInvokable]
public void OnError(string message) {
  // Something
}
@TanayParikh TanayParikh added area-blazor Includes: Blazor, Razor Components feature-blazor-jsinterop This issue is related to JSInterop in Blazor labels Nov 19, 2021
@javiercn
Copy link
Member

@Socolin thanks for contacting us.

This is a dupe of #38517. Consider upvoting that issue instead

@javiercn javiercn added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Nov 22, 2021
@ghost ghost added the Status: Resolved label Nov 22, 2021
@Socolin
Copy link
Author

Socolin commented Nov 22, 2021

@javiercn What is the dupe issue # ? You linked this one in your comment

@javiercn
Copy link
Member

javiercn commented Nov 22, 2021

#31151

@ghost ghost locked as resolved and limited conversation to collaborators Dec 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-jsinterop This issue is related to JSInterop in Blazor ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants