🎓 All Courses | 📚 Blazor WASM Syllabus
Stickipedia University
📋 Study this course on TaskLoco

Managing shared state across components in a Blazor WASM SPA requires a deliberate strategy. The simplest and most idiomatic approach is an AppState service registered as a singleton.

Creating AppState

public class AppState
{
    public string? UserName { get; private set; }
    public int CartItemCount { get; private set; }
    public event Action? OnChange;

    public void SetUser(string name)
    {
        UserName = name;
        NotifyStateChanged();
    }

    public void AddToCart()
    {
        CartItemCount++;
        NotifyStateChanged();
    }

    private void NotifyStateChanged() => OnChange?.Invoke();
}

Register and Consume

// Program.cs
builder.Services.AddSingleton<AppState>();

// Component:
@inject AppState State
@implements IDisposable

protected override void OnInitialized() => State.OnChange += StateHasChanged;
public void Dispose() => State.OnChange -= StateHasChanged;

Always unsubscribe in Dispose to prevent memory leaks.


YouTube • Top 10
Blazor WASM: State Management with AppState
Tap to Watch ›
📸
Google Images • Top 10
Blazor WASM: State Management with AppState
Tap to View ›

Reference:

Wikipedia: State Management

image for linkhttps://en.wikipedia.org/wiki/State_management

📚 Blazor WASM — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT