aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

`bind:after` with asynchronous method causes multiple renders when done.

Open KristofferStrube opened this issue 2 years ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

When we use bind:after with an asynchronous method then that invokes multiple renders once the method is done. An example of this could be the following sample razor page.

<input @bind=@_myText @bind:after="AsynchronousTask" />

@code {
    private string _myText = "Start Value";

    private async Task AsynchronousTask()
    {
        await Task.Delay(4000);
    }

    protected override void OnAfterRender(bool firstRender)
    {
        Console.WriteLine("rendered!");
    }
}

Expected Behavior

If we observe the console output we would expect it log "rendered!" 3 times.

  • 1 time when the page loads.
  • 1 time when _myText is updated.
  • 1 time when the async task finishes.

What we actually see is that it logs "rendered!" 5 times.

  • 1 time when the page loads.
  • 1 time when _myText is updated.
  • 3 times when the async task finishes.

Steps To Reproduce

I have made the following repo that reproduces this behavior: https://github.com/KristofferStrube/BlazorNet7bindAfterRenderSample

You can test this without building it yourself as it is hosted on Github Pages here: https://kristofferstrube.github.io/BlazorNet7bindAfterRenderSample/

Exceptions (if any)

No response

.NET Version

7.0.100-preview.7.22377.5

Anything else?

No response

KristofferStrube avatar Aug 10 '22 16:08 KristofferStrube

1 time when the async task is invoked.

Just a small nit pick. I dont expect this. What I expect is "1 time when _myText is updated before AsynchronousTask is invoked".

egil avatar Aug 10 '22 17:08 egil

@egil totally agree. My mistake. 😅

KristofferStrube avatar Aug 10 '22 17:08 KristofferStrube