BLState 2.0.0
dotnet add package BLState --version 2.0.0
NuGet\Install-Package BLState -Version 2.0.0
<PackageReference Include="BLState" Version="2.0.0" />
<PackageVersion Include="BLState" Version="2.0.0" />
<PackageReference Include="BLState" />
paket add BLState --version 2.0.0
#r "nuget: BLState, 2.0.0"
#:package BLState@2.0.0
#addin nuget:?package=BLState&version=2.0.0
#tool nuget:?package=BLState&version=2.0.0
BLState Simple State Management For Blazor
Simple source generator library for working with global state and updates in Blazor. Use dataannotations to declare stores and values.
Prerequisites
Blazor WASM or Server project based on .NET 6 or higher.
How to use BLState
Download package
- Download the package trough nuget store.
- Or add via CLI - dotnet add package BLState.
- Or add a reference directly in the project file.
<PackageReference Include="BLState" Version="2.0.0" />.
Note: If you are using Visual Studio you might have to restart Visual Studio after downloading the package for the first time to trigger start of source generation
Register Services
First register BLState so the stores so they can be used with dependency injection. Each store registers with a scoped lifetime
// In program.cs
using BLState;
builder.Services.AddBLStore();
Add a Store
Create a public partial class and anotate it with the attribute [BLStore]. For each value in the store add a private field with the annotation BLValue.
using BLState;
...
[BLStore]
public partial class CountStore
{
[BLValue]
private int count = 1;
[BLValue]
private int _multiplier = 3;
}
The example above creates a store named CountStore with the properties Count and Multiplier.
Use in a component
To use in a component
- Inject the store (don't forget to add a using directive to where you have defined your store).
- Add a BLUpdate component to subscribe to changes in a specific store. This component requires two arguments. A store it will listen to and a OnUpdate action that will be triggerd when an update occurs.
- Access the values in the store by the generated property names.
- Update a value, this change will be reflected in all components that uses the store.
@inject CountStore CountStore
<BLUpdate Store="CountStore" OnUpdate="StateHasChanged" />
<p>Current count: @CountStore.Count</p>
<button @onclick="MultiplyValue">Multiply</button>
@code {
private void MultiplyValue()
{
// Sets a new value to the count filed in CountStore
CountStore.Count *= CountStore.Multiplier;
}
}
Trigger update on reference type
When making a change to a reference type without changing the actual reference the store needs to be told that a update occured. To do this use the Update or UpdateAsync method.
Example a store with a list.
[BLStore]
public partial class UserStore
{
[BLValue]private List<string> names = new List<string>();
}
Then in the compoenent
using BLState;
@inject UserStore UserStore
...
UserStore.Update(x => x.Names.Add("Scott"));
//Or the async variant
await UserStore.UpdateAsync(async x => x.Names.Add(await GetName()));
To manually trigger that the store has been updated call the method InvokeUpdates on the store.
UserStore.Names.Add("Scott");
UserStore.InvokeUpdates();
Custom property names
For the need of custom property names of the values in the store add a string to the BLValue attribute
[BLValue(PropertyName = "CustomName")] private string secretName = "";. The public property will in this case be named CustomName.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- Microsoft.AspNetCore.Components (>= 6.0.2)
-
net7.0
- Microsoft.AspNetCore.Components (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.