question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoking an static method of JS Class inside OnAfterRenderAsync in First Render throw undefined class error

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I try to define a js class with static methods and then invoke one of its static methods through JSInterop in Blazor WebAssembly Razor Components inside OnAfterRenderAsync. It give an error that the method is not exist since the class is undefined. Example: 1- JS Class (I placed it inside Head of document )

class JSLibrary {
            static _DotNetObjectReference;
            static CreateInstance(instance) {
                JSLibrary._DotNetObjectReference = instance;
            }
            static GetScores() {
                var scores = _DotNetObjectReference.invokeMethod("GetScore", 0, 21);
                console.log(scores);
            }
            static GetCredits() {
                var credits = _DotNetObjectReference.invokeMethod("GetCredit");
                console.log(credits);
            }
            static GetAverage() {
                _DotNetObjectReference.invokeMethod("GetAverage");
            }
        }

2- Invocation of static method inside OnAfterRenderAsync :

protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _jsmodule = DotNetObjectReference.Create(jSModule);// create the reference and pass to Static methods in js
            await JS.InvokeVoidAsync("JSLibrary.CreateInstance",_jsmodule);
        }
        await base.OnAfterRenderAsync(firstRender);
    }

Error:

Microsoft.JSInterop.JSException: Could not find 'JSLibrary.CreateInstance' ('JSLibrary' was undefined).
      Error: Could not find 'JSLibrary.CreateInstance' ('JSLibrary' was undefined).

Expected Behavior

Expected to work as methods defined inside modules or global namespace in Java Script.

Steps To Reproduce

Create a Blazor web assembly using template and then add the class definition to head of index.html and put OnAfterRenderAsync at one of the components and test it (go to url regarded to component and check the developer logs in browser to see error)

Exceptions (if any)

No response

.NET Version

6.0.201

Anything else?

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
javiercncommented, Mar 9, 2022

window.JSLibrary = JSLibrary

1reaction
javiercncommented, Mar 9, 2022

@abdollahkahne can you create a minimal repro project as a public github repository so that we can make progress in determining if there’s something wrong here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling imported class' static method throws a TypeError error
I've already tried exporting and importing the modules like this: // Exporting the module (Bot.js) module.exports = { Bot }; // Importing the ......
Read more >
Call JavaScript functions from .NET methods in ASP. ...
This article explains how to invoke JavaScript (JS) functions from .NET. For information on how to call .NET methods from JS, see Call...
Read more >
Communicating between .NET and JavaScript in Blazor ...
This method will invoke the specified JavaScript function. It requires 1 parameter; A string to specify which JavaScript function to run. After ...
Read more >
static - JavaScript - MDN Web Docs - Mozilla
The static keyword defines a static method or field for a class, ... Public static features are declared using the static keyword.
Read more >
Pros and Cons of Using JavaScript Interop in Blazor
This blog post explains using JavaScript interop concepts in the Blazor framework, its advantages, and its limitations with code examples.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found