mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.22k stars 139 forks source link

Fluxor on Blazor .NET 8 Preview RC2 #456

Closed afmuller closed 8 months ago

afmuller commented 8 months ago

Hi

I've set up a Blazor app on the latest preview of .NET 8 (RC2). I get no errors in the logs, but the Fluxor dispatcher does not dispatch anything. Also, Redux Dev Tools plugin does not detect the app.

I've created a quick project to demo the problem. Full source code at: https://github.com/afmuller/FluxorTestAspNet8

Program.cs:

using Fluxor;
using FluxorTestAspNet8.Components;
using FluxorTestAspNet8.Stores;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

// Add Fluxor
builder.Services.AddFluxor(options =>
{
    options.ScanAssemblies(typeof(AppState).Assembly);
    options.UseReduxDevTools();
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

app.Run();

If you click on the 'Send Message to Console via Fluxor', nothing happens. image

Fluxor state etc below:

using Fluxor;

namespace FluxorTestAspNet8.Stores;

[FeatureState]
public record AppState
{
    public string LastMessage { get; set; } = "None yet";

}

public record SendMessageToConsole(string Message) { };

public static class AppReducers
{
    [ReducerMethod]
    public static AppState SendMessageToConsole(AppState state, SendMessageToConsole action)
    => state with
    {
        LastMessage = action.Message
    };

}

public class AppEffects
{

    [EffectMethod]
    public async Task SendMessageToConsole(SendMessageToConsole action, IDispatcher dispatcher)
    {
        Console.WriteLine(action.Message);  
    }

}
afmuller commented 8 months ago

Hi

I found my problem!

I was missing the StoreInitializer in the Routes.razor file.

<Fluxor.Blazor.Web.StoreInitializer />

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
</Router>