Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Options for State Management in Blazor

What libraries/techniques are available in Blazor for State management for webassembly (wasm).

It would be nice to know pros and cons of different approaches.

like image 514
Neil Avatar asked May 12 '21 00:05

Neil


People also ask

What is state management in Blazor?

Abstract: State Management is one of the much-needed features in modern web applications. In this Blazor Tutorial, we will be discussing about the state management features provided by ASP.NET Core Blazor in browsers, which can save a round trip to the server. Blazor is a framework for building interactive client-side Web UI using .NET.

Is Blazor server stateful or stateful?

Blazor Server is a stateful app framework. Most of the time, the app maintains a connection to the server. The user's state is held in the server's memory in a circuit. Examples of user state held in a circuit include: The hierarchy of component instances and their most recent render output in the rendered UI.

What is an example of user state in Blazor?

Examples of user state modeled in the URL include: The ID of a viewed entity. The current page number in a paged grid. The contents of the browser's address bar are retained if the user manually reloads the page. For information on defining URL patterns with the @page directive, see ASP.NET Core Blazor routing.

How do I handle state in Blazor WebAssembly?

An example of this is provided by the Microsoft.ASpNetCore.ProtectedBrowserStorage package. Another way to handle state is by calling an API and persisting it on the server. How it is persisted is up to you: options range from SQL, NoSQL to simple caches like Redis. BlazorState.WasmRemote.Server is an ASP.NET hosted Blazor WebAssembly app.


1 Answers

Redux (Fluxor)

Redux/Flux pattern has become a leader in state management in JS word; so it would make sense to adopt this best practice.

Luckily there is already a Fluxor library that does just that; and it does have the most github stars 379; although it is still a relatively new area; so it remains to be seen what ends up being the most adopted approach later on.

There is a great intro video along with source code

Official docs:

  • State, actions, and reducers
  • Effects
  • Redux Dev Tools

An in depth article.

Blazor-State

Blazor-State

Pros: uses MediatR for messaging

See also

Overviews of State Management Approaches

https://chrissainty.com/mobile-blazor-bindings-state-management-and-data/

https://jonhilton.net/blazor-state-management/

https://docs.microsoft.com/en-us/aspnet/core/blazor/state-management

nice but mostly messaging: https://jasonwatmore.com/post/2020/07/30/aspnet-core-blazor-webassembly-communication-between-components

keywords: passing data between blazor wasm webassembly components child parent attributes events handling

like image 197
Neil Avatar answered Sep 19 '22 15:09

Neil