BlazorStateProvider 1.0.0
dotnet add package BlazorStateProvider --version 1.0.0
NuGet\Install-Package BlazorStateProvider -Version 1.0.0
<PackageReference Include="BlazorStateProvider" Version="1.0.0" />
paket add BlazorStateProvider --version 1.0.0
#r "nuget: BlazorStateProvider, 1.0.0"
// Install BlazorStateProvider as a Cake Addin #addin nuget:?package=BlazorStateProvider&version=1.0.0 // Install BlazorStateProvider as a Cake Tool #tool nuget:?package=BlazorStateProvider&version=1.0.0
BlazorStateProvider
BlazorStateProvider is a state management library for Blazor applications that simplifies the management of state using local and session storage. It provides a clean and easy-to-use API for managing application state across components, ensuring that state is persisted as needed.
Features
State Persistence: Supports both local and session storage for persisting application state, allowing you to maintain state across user sessions or just within a single session.
Simple API: Provides a straightforward interface for managing state with methods to get, set, remove, and reset state, making it easy to use throughout your Blazor application.
State Change Notifications: Implements a subscription mechanism that notifies components of state changes, enabling a reactive programming model for better UI updates.
Type Safety: Utilizes generics to ensure type-safe state management, reducing the risk of runtime errors when retrieving and storing state values.
Installation
You can install the BlazorStateProvider NuGet package using the .NET CLI:
dotnet add package BlazorStateProvider
Or by adding it via the NuGet Package Provider in Visual Studio / Rider.
Getting Started
1. Register the Services
To use the BlazorStateProvider in your application, register the services in your Program.cs file:
using BlazorStateProvider.Extensions;
public class Program
{
public static void Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
// Register BlazorStateProvider services
builder.Services.AddBlazorStateProvider();
await builder.Build().RunAsync();
}
}
2. Inject and Use the State Provider
You can now inject the state Providers into your Blazor components.
@page "/example"
@inject IStateProvider StateProvider
<h3>State Management Example</h3>
<button @onclick="SaveState">Save State</button>
<button @onclick="LoadState">Load State</button>
@code {
private async Task SaveState()
{
await StateProvider.SetStateAsync("exampleKey", "Hello, World!");
}
private async Task LoadState()
{
var value = await StateProvider.GetStateAsync<string>("exampleKey");
Console.WriteLine($"Loaded State: {value}");
}
}
API Reference
Interfaces
IStateProvider
: The main interface for managing state in Blazor applications.ILocalStorageStateProvider
: Interface for managing state using local storage.ISessionStorageStateProvider
: Interface for managing state using session storage.
Methods
SetStateAsync<T>(string key, T value)
: Sets the state value for the specified key.GetStateAsync<T>(string key)
: Gets the state value for the specified key.RemoveStateAsync(string key)
: Removes the state value for the specified key.ResetStateAsync()
: Resets the state by removing all stored values.Subscribe<T>(Action callback)
: Subscribes to state change notifications for the specified key.Unsubscribe<T>(Action callback)
: Unsubscribes from state change notifications for the specified key.
Example
Here’s a simple example of how to use the BlazorStateProvider in your components:
@page "/example"
@inject ILocalStorageStateProvider LocalStorageProvider
<h3>Local Storage Example</h3>
<button @onclick="SaveToLocalStorage">Save to Local Storage</button>
<button @onclick="LoadFromLocalStorage">Load from Local Storage</button>
@code {
private async Task SaveToLocalStorage()
{
await LocalStorageProvider.SetStateAsync("username", "JohnDoe");
}
private async Task LoadFromLocalStorage()
{
var username = await LocalStorageProvider.GetStateAsync<string>("username");
Console.WriteLine($"Loaded Username: {username}");
}
}
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please submit an issue or a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions 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. |
-
net8.0
- Blazored.LocalStorage (>= 4.5.0)
- Blazored.SessionStorage (>= 2.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 78 | 11/1/2024 |