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.

Make DOM element JavaScript lifecycle callbacks declarable in Razor syntax

See original GitHub issue

Inspired by @SQL-MisterMagoo’s issue #17472, I suggest the following generalized solution:

Describe the solution you’d like

When defining a HTML element to be rendered in the browser in Blazor, it should be possible to specify JavaScript functions that are executed in the context of that HTML element, when it is first rendered, rendered, and disposed/removed from the DOM.

For example, the Dropdown Bootstrap component could look like this then (this is just for illustration purposes, not a complete implementation):

<div class="dropdown">
  <button @onFirstRender="boostrapInteropt.dropdownInit(@dotnetHelper)" 
          @onRender="boostrapInteropt.dropdownRender()"
          @onDispose="boostrapInteropt.dropdownDispose()" 
          class="btn btn-secondary dropdown-toggle" ...>
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

@code {
    private DotNetObjectReference dotnetHelper = DotNetObjectReference.Create(...);
}

where this in each function call is bound to the rendered DOM element. This assumes an JavaScript object exists such as:

 window.boostrapInteropt { 
   dropdownInit: function(dotnetHelper) {
        $(this).dropdown();
        // ...
   },
   dropdownRender: function() { 
        $(this).dropdown('update');
        // ...
   },
   dropdownDispose: function() { 
        $(this).dropdown('dispose');
        // ...
   }
}

Note, it should be possible to pass arguments to the JavaScript functions as it is through the normal JsRuntime’s invoke methods. In the example above, we are passing a dotnetHelper callback object.

This will enable simple and consistent integration with existing JavaScript based components, that usually surface methods such as init and destroy/dispose. It ensures JavaScript objects are correctly cleaned up and functionality is available as soon as possible to the end user.

Why not use razor lifecycle methods and JsRuntime.InvokeAsync(...)?

The problem with OnAfterRender is that it is first called after the element is visible to the users. In Blazor Server scenarios, the round-trip network delay can be high enough, that the user will experience elements not working probably or visually moving on the screen, e.g. if using popper.js to position elements.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SQL-MisterMagoocommented, Dec 2, 2019

That should give us plenty of time to work on a proof of concept 😁 I’ve got a very basic version working now, we can always provide an alternative js file for the brave ones to play with.

1reaction
egilcommented, Dec 2, 2019

That is fair @javiercn.

One additional point I would like to make is that this will make it easier to integrate existing JavaScript components with Blazor, which will likely help increase adoption of Blazor. The important point is that it will make it possible to make the integration consistent between Blazor Server and Blazor Wasm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use JavaScript inside Razor Syntax
I need to get my element ID and based on this, I try to get Image URK from list of images of my...
Read more >
ASP.NET Core Razor component lifecycle
Component lifecycle events of a Razor component in Blazor. DOM event processing: The event handler is run. If an incomplete Task is returned ......
Read more >
Lifecycle Hooks in Web Components
An element can be deleted from the DOM. All of the above are called the element's lifecycle, and we can hook into key...
Read more >
Creating Web Components — Lifecycle Callbacks
Element deleted from the DOM. Web Component has lifecycle hooks are callback functions that capture these lifecycle events and let us handle ...
Read more >
The custom element lifecycle
There's another lifecycle method related to connection and disconnection, which is the "adoption" callback. This gets called when your element is moved from...
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