Skip to content

Breaking changes in .NET 6 RC1 affecting Component State #23340

@jonhilt

Description

@jonhilt

Looks like the syntax for preserving state between prerendering and rendering on the client has changed in RC 1.

Specifically ComponentApplicationState is now called PersistentComponentState and the usage has changed slightly.

It now seems you need to grab the result of ApplicationState.RegisterOnPersisting() and store that as a field which you can then call .Dispose() on when disposing of your component.

@code {

    private ProductListModel _model;
    private PersistingComponentStateSubscription _subscription;

    protected override async Task OnInitializedAsync()
    {
        _subscription = ApplicationState.RegisterOnPersisting(PersistModel);

        if (ApplicationState.TryTakeFromJson("model", out ProductListModel model))
            _model = model;
        else
            _model = await _api.ProductList(new ProductListQuery());
    }

    private Task PersistModel()
    {
        ApplicationState.PersistAsJson("model", _model);
        return Task.CompletedTask;
    }

    public void Dispose()
    {
        _subscription.Dispose();
    }

}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions