1

So what I am doing here is, I have a progress bar in my child component(RMSBaseComponent.razor) which is surrounded by a condition variable "IsLoading"

<div style="padding:20px">

@* Page Title *@
<h3>@PageTitle</h3>
<MatDivider></MatDivider>

@* Loader *@
@if (IsLoading)
{
    <MatProgressBar Indeterminate="true"></MatProgressBar>
}
</div>

I am making this Child in Parent(AddNewCity.razor) as

<RMSBaseComponent PageTitle="Add New City" @ref="BaseComponent">

</RMSBaseComponent>

Here I am getting the reference of child in "BaseComponent" variable but when I change the value of "IsLoading" from parent component class (AddNewCity.cs) it does not update the UI and not showing "MatProgressBar" UI. I am changing value as below

public partial class AddNewCity
{
    private RMSBaseComponent BaseComponent { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);

        Thread.Sleep(3000);
        BaseComponent.IsLoading = true;
    }
}

Any help will be appriciated.

Thank you

1

1 Answer 1

1

You should call StateHasChanged() after Thread.Sleep()

public partial class AddNewCity
    {
        private RMSBaseComponent BaseComponent { get; set; }

        protected override void OnAfterRender(bool firstRender)
        {
            base.OnAfterRender(firstRender);

            Thread.Sleep(3000);
            BaseComponent.IsLoading = true;
            StateHasChanged();
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I would also advise that you consider passing such references as a Parameter to the child component - if the child needs to mutate the state of their parent even some two-way binding or at least an event might be in order. Personally, I prefer raising events from the child components so their parents can decide what to do with that (like, toggle a loading sign, or fetch other data).
StateHasChanged(); saved my life. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.