mrpmorris / Fluxor

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

BaseState class with nice to have stuff like IsLoading #188

Closed devantler closed 3 years ago

devantler commented 3 years ago

I've been using Fluxor for quite a while now and noticed that I always create my own BaseState classes with shared properties across States. Mostly the IsLoading boolean to more elegantly show loader components in Blazor.

It would nice if Fluxor came with a BaseState class with nice to have properties to guide users in the right direction, instead of having us create a custom one for each project. My thoughts are that some properties like IsLoading are always useful, while others aren't. But having those default properties accessible from the get-go, removes some boilerplate code when setting up Fluxor.

E.g. something basic like this:

public class State 
{
    public bool IsLoading {get; set;}

    public State(bool isLoading)
    {
        IsLoading = isLoading;
    }
}

This then allows new users to use this base state to more easily follow the recommended practice like so:

public class MyCustomState : State
{
    public int SomeCustomProperty {get; set; }

    public MyCustomState(int someCustomProperty, bool isLoading) : base(isLoading) 
    {
        SomeCustomProperty = customProperty;
    }
}

@mrpmorris What do you think of such an approach?

Tailslide commented 3 years ago

I took a different approach.. base classes for getting data and finished getting data. My actions are all subclasses of these but then I have a reducer on the base class that updates a central loading state and error state. I only need to make one loading and error reporting UI for the whole application instead of handling every state.

Might be interesting to do a template pack with samples of different ways to handle state: https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates