Skip to content

Sharaf-Mansour/Arora.Blazor.StateContainer

Repository files navigation

Arora.Blazor.StateContainer

Arora.Blazor.StateContainer is a .NET library that provides a simple state container implementation for Blazor applications. The library defines a StateContainer class that can be used to store and manage application state, and an extension method AddStateContainer() that can be used to register the StateContainer with the dependency injection container.

Installation

You can install the Arora.Blazor.StateContainer library using the .NET CLI:

dotnet add package Arora.Blazor.StateContainer

Usage

Top-level statements

If you're using C# 9 or later with top-level statements, you can use the Arora.Blazor.StateContainer library as follows:

using Arora.Blazor.StateContainer;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddStateContainer();

var app = builder.Build();

app.Run();

Old-style program and Startup

If you're using an older version of C# or the Program.cs and Startup.cs files in your project, you can use the Arora.Blazor.StateContainer library as follows:

using Arora.Blazor.StateContainer;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddStateContainer();
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Steps

  1. Add the following using statement:
using Arora.Blazor.StateContainer;
@using Arora.Blazor.StateContainer;
  1. In your Blazor component, use the @inject directive to inject the StateContainer instance:
@inject StateContainer State
  1. Use the StateContainer instance in your component to update the state:
<p>Count: @count</p>
<button @onclick="IncrementCount">Increment</button>

@code {
    private int count = 0;

    private void IncrementCount()
    {
        count++;
        State.NotifyStateChanged();
    }

    protected override void OnInitialized()
    {
        State.OnChange += StateHasChanged;
    }

    public void Dispose()
    {
        State.OnChange -= StateHasChanged;
    }
}

About

A State Container for both Blazor WASM and Server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages