Aspnetcore: [Blazor] Infinity loop and increased RAM usage - is this correct behaviour?

Created on 15 Aug 2019  路  3Comments  路  Source: dotnet/aspnetcore

Hi!

I'm not sure whether it is bug, or it actually should work like that

but this code causes infinity loop and rapid increase in RAM usage

https://i.imgur.com/xUdIWS3.png

<div>
    <label @bind="@Error"></label>
    <br />
    <label>Username:</label>
    <input type="text" @bind="Username" />
    <br />
    <label>Email:</label>
    <input type="text" @bind="Email" />
    <br />
    <label>Password:</label>
    <input type="password" @bind="Password1"  />
    <br />
    <label>Password Repeated:</label>
    <input type="password" @bind="Password2" />
    <br />
    <button onclick="@TryError()"></button>
</div>

@code{
    public string Username;
    public string Email;
    public string Password1;
    public string Password2;
    public string Error;

    async Task TryError()
    {
        Error = DateTime.Now.ToString();
        this.StateHasChanged();
    }
}

That was fresh Blazor project created with

dotnet new blazor

&

<TargetFramework>netcoreapp3.0</TargetFramework>

on

3.0.100-preview8-013656 @ Windows 10

area-blazor question

Most helpful comment

<button onclick="@TryError()"></button>

This is calling the TryError method every time the component renders.

I think you're probably looking for:

<button @onclick="TryError"></button>

All 3 comments

Perhaps it's due to your StateHasChanged() call. According to the docs, it's not necessary:

https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#event-handling

Event handlers can also be asynchronous and return a Task. There's no need to manually call StateHasChanged().

But it does seem weird.

@limefrogyank that was Indeed caused by this.StateHasChanged();

<button onclick="@TryError()"></button>

This is calling the TryError method every time the component renders.

I think you're probably looking for:

<button @onclick="TryError"></button>
Was this page helpful?
0 / 5 - 0 ratings