PsyDuck
PsyDuck

Reputation: 99

OnAfterRender OnAfterRenderAsync in Blazor Component (Radzen)

I have a list of users and I want to show the number of the list in RadzenTextBox. something like this:

<RadzenTextBox @bind-Value=@total Style="width: 80px; text-align: right" Change=@(args => OnChange(args)) />

So @total is the number of users and I set it in OnInitialized()

Then I add one user to the list and I set @total again in OnAfterRender() or OnAfterRenderAsync(). @total was changed (I console in OnAfterRender() ) but my view dont re-render.

It only renders when I add the second time. I dont know why

Upvotes: 2

Views: 2433

Answers (1)

JanB
JanB

Reputation: 308

Problem is that when you set total in OnAfterRender (it is literally after render), the component does not know that state was changed again and will not reflect total's new value.

It explains why total is always 1 "render" behind.

To solve that, you can:

  • set total in OnParametersSet(Async) or during EventCallback
  • after you set total, call StateHasChanged()

Note: You can call StateHasChanged() even in OnAfterRender though I do recommend avoiding that unless there is no other option.

I also recommend checking this brilliant poster by @RobertHaken, which explains everything.

Upvotes: 2

Related Questions