• API
    • CPF
  • Blog
  • Contact

Contact Us

Give us a call or fill in the form below and we will contact you. We endeavor to answer all inquiries within 24 hours on business days.

Edit Content

     

    Technology

    State Management in Blazor: Best Practices and Strategies

    March 3, 2025 Weslei Gomes de Sousa, Bruno de Souza Melo

    State management is a critical aspect of building Blazor applications, as it determines how data is stored, updated, and shared across components. Unlike JavaScript frameworks such as React and Angular, Blazor leverages C# for managing state, offering several built-in and advanced strategies. This article explores state management approaches in Blazor, covering built-in options and external libraries for more scalable applications.

    State in Blazor

    State in Blazor refers to data that influences the UI rendering and behavior of a component. Since Blazor is component-based, each component can maintain its state internally or share state across multiple components.

    Blazor provides two execution models: (a) Blazor Server: State is managed on the server, and UI updates are pushed via SignalR; (b) Blazor WebAssembly: State is managed in the client’s browser, persisting only during the session unless stored explicitly.

    Built-in State Management Techniques

    1. Component State (Private Fields)

    Each Blazor component can maintain internal state using private fields.

    @code {
        private int count = 0;
        private void Increment() => count++;
    }

    However, this state resets when the component is reloaded.

    2. Cascading Parameters

    When multiple child components need access to the same state, CascadingValue can be used.

    <CascadingValue Value="currentUser">
        <UserProfile />
    </CascadingValue>

    The child component receives the value:

    @code {
        [CascadingParameter] public User currentUser { get; set; }
    }

    3. Dependency Injection (DI) for Shared State

    State can be managed through a service registered in DI.

    Step 1: Create a State Service

    public class CounterService
    {
        public int Count { get; private set; }
        public void Increment() => Count++;
    }

    Step 2: Register Service in Program.cs

    builder.Services.AddSingleton<CounterService>();

    Step 3: Inject and Use in Components

    @inject CounterService CounterService
    <p>Count: @CounterService.Count</p>
    <button @onclick="() => CounterService.Increment()">Increment</button>

    This allows components to share the same counter state.

    Advanced State Management Techniques

    4. Using Fluxor for State Management

    Fluxor is a state management library inspired by Redux that provides predictable state updates.

    Step 1: Install Fluxor

    dotnet add package Fluxor.Blazor.Web

    Step 2: Configure Fluxor

    builder.Services.AddFluxor(options => options.ScanAssemblies(typeof(Program).Assembly));

    Step 3: Define a State and Actions

    public record CounterState(int Count);
    public class IncrementCounterAction { }
    public class CounterReducer : Reducer<CounterState, IncrementCounterAction>
    {
        public override CounterState Reduce(CounterState state, IncrementCounterAction action) => new(state.Count + 1);
    }

    Step 4: Use in Blazor Component

    @inject IDispatcher Dispatcher
    @inject IState<CounterState> CounterState
    
    <p>Count: @CounterState.Value.Count</p>
    <button @onclick="() => Dispatcher.Dispatch(new IncrementCounterAction())">Increment</button>

    This approach centralizes state updates, making debugging and testing easier.

    5. Persisting State with Local Storage

    For Blazor WebAssembly apps, state can be stored in the browser’s LocalStorage.

    Step 1: Install Blazored.LocalStorage

    dotnet add package Blazored.LocalStorage

    Step 2: Register in Program.cs

    builder.Services.AddBlazoredLocalStorage();

    Step 3: Use in Component

    @inject ILocalStorageService LocalStorage
    @code {
        private int count;
        protected override async Task OnInitializedAsync()
        {
            count = await LocalStorage.GetItemAsync<int>("counter");
        }
        private async Task Increment()
        {
            count++;
            await LocalStorage.SetItemAsync("counter", count);
        }
    }

    This ensures that state persists even after a page refresh.

    Comparing Blazor State Management Approaches

    MethodBest forScopePersistence
    Component StateSmall UI statePer component❌ No
    Cascading ParametersShared parent-child stateComponent tree❌ No
    Dependency InjectionShared global stateApplication-wide❌ No
    Fluxor (Redux pattern)Complex appsApplication-wide❌ No
    LocalStoragePersisting user dataClient-side✅ Yes

    Conclusion

    Choosing the right state management strategy in Blazor depends on the complexity of the application. For simple apps, built-in state management techniques such as CascadingParameters and dependency injection work well. For larger applications, Fluxor provides a structured approach similar to Redux. When persistence is required, LocalStorage integration ensures that user data is retained across sessions.

    By applying these best practices, Blazor developers can build scalable, maintainable, and high-performance applications with efficient state management.

    • Weslei Gomes de Sousa

      With over 20 years of experience in IT solutions architecture, has strong expertise in .NET and Azure and high proficiency in managing relational and non-relational databases. Techs: Azure Architecture, Azure DevOps (CI/CD, Artifacts, Git), Docker, Azure Storage, B2C, Azure Service Bus, RabbitMQ, AWS Cloud, Web Applications/Frontend (Angular, Blazor, React, Razor Pages), TypeScript, Python, ASP.NET Web API (REST), MAUI (Mobile Development), Entity Framework Core, C#, Windows Applications (WPF, WinForms, UWP), Java, among others.

    • Bruno de Souza Melo

      Over 20 years of experience delivering innovative IT solutions for public and private sectors, enhancing user welfare and societal impact.

      Expertise
      Development & Infrastructure: Extensive experience with Visual Studio (2003–2022), Azure, GCP, AWS, messaging platforms (Azure Service Bus, Pub/Sub, RabbitMQ), Azure DevOps (CI/CD, Git, Artifacts), Docker, Microsoft 365, SharePoint, IntelliJ IDEA, and more.
      Data Tools: Proficiency in Spark, Jupyter/Zeppelin, and Hue for Impala/Hive (HDFS). Databases: Skilled in Microsoft SQL Server, PostgreSQL, MongoDB, Oracle, and others.
      Technologies & Languages: Expertise in web development (Blazor, React, Angular, Razor Pages), TypeScript, Python, ASP.NET Web API, Xamarin, Entity Framework Core, C#, Java/Spring, and Windows applications (WPF, WinForms, UWP).

      Methodologies & Architecture
      Agile methodologies (Scrum, Kanban) for efficient project management. Solution architecture, including business modeling (BPMN, C4 Model) and technology frameworks (Clean Architecture).

      Industry Experience
      Diverse domain knowledge across fast food, transportation, banking, hospitality, contracts, and more.

    Post navigation

    Previous

    Search

    Categories

    • Technology (7)

    Recent posts

    • State Management in Blazor: Best Practices and Strategies
    • Understanding obj == null vs obj is null in C# and Their Performance Impact
    • 500x Faster: Avoid Queries in Loops with C# Entity Framework for Peak Performance

    Tags

    #ai #artificialintelligence #judiciary #speedtrial #ai #artificialintelligence #publicsector BestPractices Blazor c# CleanCode converter CSharp dataperformance datetime DotNet ef entity entityframework enum highperformance json keys nuvtools nuvtools.common serialization Web

    Related posts

    Technology

    Understanding obj == null vs obj is null in C# and Their Performance Impact

    January 30, 2025 Bruno de Souza Melo, Weslei Gomes de Sousa

    When working with null checks in C#, developers often use one of the following approaches: Both methods check if an object reference is null, but there are important differences between them, especially regarding operator overloading, reliability, and performance. In this article, we will explore these differences and analyze their performance impact. 1. The Equality Operator […]

    Fuel your business growth and witness immediate results today.

    Menu
    • Home
    • API
    • Blog
    • Contact

    © Nuv Tools Technology. All Rights Reserved.

    © Nuv Tools Technology.
    All Rights Reserved.