I have no previous Blazor and very little asynchronous programming experience. I'm exploring using Blazor for a google maps project and trying to add a geojson point to the map using the google maps javascript api. I wasn't sure of the best way to do this, so my plan was to update the inner html of a hidden div in a blazor component and reference that value in my google map api call. When I call AddMapData (which gets a geojson string from my db) to set the html of the inner div in both OnInitializedAsync and OnAfterRenderAsync everything works great:
@page "/map"
@inject IJSRuntime JSRuntime
@using DataAccessLibrary
@inject IGeoData _db
<div id="info-box"></div>
<div hidden id="mapData">@mapData </div>
<h3>Map</h3>
<div id="map" ></div>
@code {
private string mapData;
private List<string> data;
async Task<bool> AddMapData()
{
data = await _db.GetGeoData();
mapData = data.First<string>();
mapData = mapData.Substring(1, mapData.Length - 2);
return true;
}
protected override async Task OnInitializedAsync()
{
await AddMapData();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await AddMapData();
await JSRuntime.InvokeVoidAsync("initMap", null);
}
}
}
Why do I have to call AddMapData in both? My first thought was to only call it in the OnAfterRenderAsync override, but the div.innerhtml value is "". Any thoughts or input is appreciated.