Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/saileshrijal/blazorstateprovider

A lightweight state management library for Blazor applications. Easily manage global and scoped states with support for session and local storage.
https://github.com/saileshrijal/blazorstateprovider

blazor blazor-components blazor-webassembly csharp dependency-injection dotnet local-storage opensource session-storage state-management state-persistence web-development

Last synced: 3 months ago
JSON representation

A lightweight state management library for Blazor applications. Easily manage global and scoped states with support for session and local storage.

Awesome Lists containing this project

README

        

# 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:

```bash
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:

```csharp
using BlazorStateProvider.Extensions;

public class Program
{
public static void Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add("#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.

```csharp
@page "/example"
@inject IStateProvider StateProvider

State Management Example

Save State
Load State

@code {
private async Task SaveState()
{
await StateProvider.SetStateAsync("exampleKey", "Hello, World!");
}

private async Task LoadState()
{
var value = await StateProvider.GetStateAsync("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(string key, T value)`: Sets the state value for the specified key.
- `GetStateAsync(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(Action callback)`: Subscribes to state change notifications for the specified key.
- `Unsubscribe(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:

```csharp
@page "/example"
@inject ILocalStorageStateProvider LocalStorageProvider

Local Storage Example

Save to Local Storage
Load from Local Storage

@code {
private async Task SaveToLocalStorage()
{
await LocalStorageProvider.SetStateAsync("username", "JohnDoe");
}

private async Task LoadFromLocalStorage()
{
var username = await LocalStorageProvider.GetStateAsync("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](LICENSE) file for details.