Home > OS >  Javascript Array to Blazor component
Javascript Array to Blazor component

Time:10-02

How would I get an array from a Javascript function to a blazor component? Here in the component I call the Javascript function ,initializeJs.

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        await JS.InvokeVoidAsync("initializeJs", null);
        StateHasChanged();
    }
}

Then I get an array from a ARCGIS service in my JavaScript.

function initializeJs(){ var dataArray = results.features}

The ArrayHow do I send the dataArray back to the Blazor component?

CodePudding user response:

You can do like this:

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var dataArray = await JS.InvokeAsync<string[]>("initializeJs", null);
            Console.WriteLine($"{dataArray[0]}");
            StateHasChanged();
        }
    }

and in your js you can just return the dataArray

function initializeJs() {
    var dataArray = ["A", "B", "C"];
    return dataArray;
}
  • Related