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}
How 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;
}