Skip to content

[Question] What is the correct way to call StateHasChanged when a collection changes (Blazor)? #3229

@geometrikal

Description

@geometrikal

I'm using ReactiveUI.Blazor to implement MVVM in a Blazor WASM application.

View models are made available using the built-in DI. They inherit from ReactiveObject and I'm using SourceList for the observable collection, e.g.

public SourceList<int> MyListOfNumbers {get; set;} = new()

The views inherit from ReactiveInjectableComponentBase<TViewModel>.

If any properties are changed in the view model the component will automatically update. However if the SourceList changes, it does not. What is the correct way to watch for SourceList changes and update the component? Currently I do the following inside OnInitialised in the view:

this.WhenActivated(disposable =>
{
    ViewModel.MyListOfNumbers.Connect().Subscribe(set =>
    {
        InvokeAsync(StateHasChanged);
    }).DisposeWith(disposable);
}

Is this correct?

(Can someone alse explain what is being done inside OnAfterRender in ReactiveInjectableComponentBase to hook up the properties to StateHasChanged)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions