AugusteVN.Razor.PersistState 1.0.2

dotnet add package AugusteVN.Razor.PersistState --version 1.0.2
                    
NuGet\Install-Package AugusteVN.Razor.PersistState -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AugusteVN.Razor.PersistState" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AugusteVN.Razor.PersistState" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="AugusteVN.Razor.PersistState" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AugusteVN.Razor.PersistState --version 1.0.2
                    
#r "nuget: AugusteVN.Razor.PersistState, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=AugusteVN.Razor.PersistState&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=AugusteVN.Razor.PersistState&version=1.0.2
                    
Install as a Cake Tool

Persist Component State

Blazor has an unfortunate double rendering issue when pre-rendering is enabled. The PesistStateProvider.cs helps prevent double HTTP calls that occur on the component's lifecycle methods which get triggered twice.

Usage Example

Inheritance

public class BlogHttpClient : PersistStateProvider
{
    private readonly HttpClient _http;
    public BlogHttpClient(HttpClient http, PersistentComponentState appState) : base(appState)
    {
        _http = http;
    }
    
    public async Task<BlogPostResponse?> FetchSelectedBlogPost(GetBlogPostForSlugRequest request)
    {
        return await FetchAsync<BlogPostResponse?>(
            key: $"{nameof(GetBlogPostForSlug)}/{request.Slug}",
            fetchMethod: () => GetBlogPostForSlug(request));
    }

    private async Task<BlogPostResponse?> GetBlogPostForSlug(GetBlogPostForSlugRequest request)
    {
        try
        {
            return await _http.GetFromJsonAsync<BlogPostResponse>($"{ApiRoutes.BlogPosts}/{request.Slug}");
        }
        catch (Exception e)
        {
            _logger.LogWarning("Could not get blog post with slug: {slug}, {@errorMessage}", request.Slug, e.Message);
            return null;
        }
    }
    
}

Composition

builder.Services.AddSingleton<PersistStateProvider>();
public class BlogHttpClient
{
    private readonly HttpClient _http;
    private readonly PersistentComponentState _appState;
    private readonly PeristStateProvider _stateProvider;
    public BlogHttpClient(HttpClient http, PersistentComponentState appState, PeristStateProvider stateProvider)
    {
        _http = http;
        _appState = appState;
        _stateProvider = stateProvider.Create(appState);
    }
    
    public async Task<BlogPostResponse?> FetchSelectedBlogPost(GetBlogPostForSlugRequest request)
    {
        return await stateProvider.FetchAsync<BlogPostResponse?>(
            key: $"{nameof(GetBlogPostForSlug)}/{request.Slug}",
            fetchMethod: () => GetBlogPostForSlug(request));
    }

    private async Task<BlogPostResponse?> GetBlogPostForSlug(GetBlogPostForSlugRequest request)
    {
        try
        {
            return await _http.GetFromJsonAsync<BlogPostResponse>($"{ApiRoutes.BlogPosts}/{request.Slug}");
        }
        catch (Exception e)
        {
            _logger.LogWarning("Could not get blog post with slug: {slug}, {@errorMessage}", request.Slug, e.Message);
            return null;
        }
    }
}

Blazor Component

ExampleComponent.razor

@implements IDisposable
@inject PersistStateProvider PersistState
@inject PersistentComponentState AppState

<p>Count: @Counter</p>

@code {
    private int Counter { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var persistState = PersistState.Create(AppState);
        
        persistState.FetchAsync<int>(
            key: nameof(Counter),
            () => {});
    }

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

To see it in action, watch: Prevent this Blazor Prerendering ISSUE C# .NET 8.

Brought to you by: kiss-code.com.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AugusteVN.Razor.PersistState:

Package Downloads
AugusteVN.Razor.State

Helper classess and components for state management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 120 6/7/2025
1.0.1 78 6/6/2025

Undo extensions methods