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.
You can install the Arora.Blazor.StateContainer library using the .NET CLI:
dotnet add package Arora.Blazor.StateContainer
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();
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>();
});
}
- Add the following using statement:
using Arora.Blazor.StateContainer;
@using Arora.Blazor.StateContainer;
- In your Blazor component, use the
@inject
directive to inject theStateContainer
instance:
@inject StateContainer State
- 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;
}
}