Am calling api to load data under async Task OnAfterRenderAsync(bool firstRender)
But jscript is loaded before users gets loaded in UI
How can i calling js once data is loaded in Users list?
Code behind
protected override async Task OnAfterRenderAsync(bool firstRender)
{
List<User> users = await Getuser(Request);
await jsRuntime.InvokeAsync<IJSObjectReference>("import", "/js/scripts/test.js");
}
UI
<div class="user-list">
<ul>
@foreach (var user in users)
{
<li>
<h5 class="mb-25">@user.name</h5>
</li>
}
</ul>
<div>
3
Answers
In OnAfterRenderAsync() method, you are load user data (from API) to local variable. Your foreach loop works on another list declared in the component.
Use:
instead of:
Use
OnInitializedAsync
to retrieve data andOnAfterRenderAsync
for js interop.In test.js you have to export the function that adds the click events:
Then after importing the js module you have to invoke this function:
Also in UI you should check if list is null.
Blazor component lifecycle
I think you want to render the users to UI before importing the js file, if that is what you want, you need to force component to apply changes to users list after you get them by using
StateHasChanged()
:Code behind:
UI: