Skip to content

Calling an async function via onclick. #16464

@RyoukoKonpaku

Description

@RyoukoKonpaku

I have been messing around with the HttpClient so far and wanted to create an async call to an Endpoint via a button click.
Something like this.

// Something that should update once the async call is done
if(!isDone) { <text>Processing</text> }
else { <text>Done: @responseText</text> }
// Button
<button type="button" @onclick(GetClick)>Get Some Data</button>
// Function
    async Task GetClick()
    {
        responseText = await Http.GetStringAsync("/");
        isDone = true;
     }

The problem is that onClick directive doesn't allow a function that returns anything it seems and only accepts void. By changing the return type to void from Task, it works but since it's an async task the Page doesn't update it's view when the async task is done and isDone is set to true since I think the component isn't notified.

The workaround I did is to put StateHasChanged() as the last call on the async void function to manually notify the component to render.
Maybe i'm doing something wrong and there's already a way for this though. Just to clarify I guess.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions