Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
Blazor, doubts regarding modifying DOM from JavaScript

I'm trying to wrap an external JavaScript library (a drag & drop one) which makes DOM modifications, but I don't really understand how blazor interacts this way.

A simple example:

JavaScript:

function ModifyDOM(dotNetHelper, element)
{
    element.innerText = "hello";
    dotNetHelper.invokeMethodAsync("Update");
}

Blazor:

@inject IJSRuntime JS

<div @ref="list">
    @foreach (var color in colors)
    {
        <div @key="color">@color</div>
    }
</div>

@code
{
    ElementReference list;

    List<string> colors = new() { "primary", "secondary", "success", "danger", "warning", "info", "light", "dark" };
    private DotNetObjectReference<FetchData>? dotNetHelper;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender) {
            dotNetHelper = DotNetObjectReference.Create(this);

            await JS.InvokeVoidAsync("ModifyDOM", dotNetHelper, list);
        }
    }

    [JSInvokable]
    public async Task Update()
    {
        @* await Task.Yield(); *@
        await InvokeAsync(StateHasChanged); // here I'm expecting to reload de original DOM, but it's not happening
    }

    public void Dispose()
    {
        dotNetHelper?.Dispose();
    }
}

When I call StateHasChanged, I was expecting to Blazor to redraw the original RenderTree, but it's not happening. It's keeping the "hello" innerText.

--- UPDATE ---

This is a complete example, I want to wrap SortableJS for sorting lists because there is not similar implementation in Blazor

But as you can see in the video below seems that Blazor is messing up with refreshing the component (on the console log i'm writing the Blazor current lists)

Example

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You should make all the modification of the DOM in you Blazor app. Blazor has its own internal binary representation of the DOM. When it renders something, it calculates diff of current DOM representation and the new one. You change the DOM in browser, but its representaion in Blazor remains unchanged. That's why StateHashChanged() has no effect in your example.

about 4 years ago · Juan Pablo Isaza Report

0

<div @ref="list">
    @foreach (var color in colors)
    {
        <div @key="color">@color</div>
    }
</div>

In this case, the collection colors doesn't change and so "Blazor thinks" there are no changes to be applied to the DOM.

The Blazor rendering code has no idea you changed the text in JavaScript.

about 4 years ago · Juan Pablo Isaza Report

0

...how blazor interacts this way.

Blazor has its own copy of the Dom (like a 'shadow Dom' but afaik using its own implementation).

On a re-render it builds a new (partial) copy and compares that to the old one. Any differences are propagated to the actual DOM.

So when JS makes a change to the actual DOM this is 'out of sight' for Blazor and those changes will stay (usually a plus) until you F5 or make Blazor think it has changed.

A best practice would be to keep Blazor and JS away from each other, but when you want to reset your list:

  1. wrap it in a component
  2. give it a (dummy) parameter and change that.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!