1

Right now my child component only updates once the GetLeads() method in the parent component is finished. I never see the spinner or Searching text.

Parent Component:

<SearchResultComponent @ref="ChildComponent"></SearchResultComponent> 

Code:

 protected SearchResultComponent ChildComponent; public int LeadsFound { get; set; } public void GetLeads() { ChildComponent.Refresh(true, 0); var leads = _searchService.Search(searchRequest); LeadsFound = leads?.Count ?? 0; _writeFileService.WriteToFile(leads); ChildComponent.Refresh(false, LeadsFound); } 

Child Component:

@code { public bool Searching { get; set; } = false; public int LeadsFound { get; set; } = 0; public void Refresh(bool searching, int leadsFound) { Searching = searching; LeadsFound = leadsFound; StateHasChanged(); } } @if (Searching) { <div>Searching...</div> <div> <img src="~/Content/searching-spinner.gif" /> </div> } else { <div>Leads Found: @LeadsFound</div> } 
3
  • How do you trigger GetLeads() ? Commented May 31, 2020 at 21:45
  • Why isn't _searchService.Search(searchRequest) async? Same for WriteToFile() Commented May 31, 2020 at 21:46
  • Button click. @onclick="@GetLeads" Commented May 31, 2020 at 21:48

1 Answer 1

3

my child component only updates once

You can make the eventhandler async. That allows you then to call Task.Delay(1) or Task.Yield() and that will effectuate the StateHasChanged().

The call form @onclick="@GetLeads" can remain the same. Note that you don't need the 2nd @ in there.

If your searchService.Search() was async then you wouldn't need Task.Yield()

 public async Task GetLeads() { ChildComponent.Refresh(true, 0); await Task.Yield(); // enable the render thread to run. var leads = _searchService.Search(searchRequest); LeadsFound = leads?.Count ?? 0; _writeFileService.WriteToFile(leads); ChildComponent.Refresh(false, LeadsFound); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.