mrpmorris / Fluxor

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

Documentation suggestion - emphasize prime purpose of FluxorComponent #314

Closed potomato closed 1 year ago

potomato commented 2 years ago

I've started working somewhere that has a Blazor app with Fluxor providing state (you may even remember this application..). So I and a couple of others have been getting familiar with Fluxor. Some know the flux pattern already, some don't.

The same question has been raised a couple of times to me:

To me, the key benefit of Fluxor in Blazor is the fact that injecting IState means that StateHasChanged will be invoked whenever IState changes.

If people are working with Fluxor and not clear on this early on, maybe documentation needs to be changed to put this earlier/high up in README. I appreciate that Fluxor isn't just Blazor, but they're both in the same repo.

Anyway, really I'm just a fan of Fluxor and concerned that people may not realise what it can do for them on a first look.

jimitndiaye commented 2 years ago

Also worth documenting is best practices on component nesting and state - is it always best to only inherit from FluxorComponent at the parent level (I believe yes, usually) and just pass data down to child components while they instead just inject the dispatcher and dispatch actions or should they all be fluxor components and get their own state?

mrpmorris commented 2 years ago

Passing state down through [Parameter] in Blazor causes a re-render of the receiving component whenever the parent component renders.

If they get their own state, it means they only render when their state changes.

jimitndiaye commented 2 years ago

@mrpmorris in that case the recommendation would be to inject an IStateSelection<TState, TData> in child components and configure the selector to call StateHasChanged when the value changes, rather than inheriting from FluxorComponent?

mrpmorris commented 2 years ago

Yes, but make sure you call InvokeAsync, just to be safe.

You could also use StateSubscriber, which automatically subscribes + unsubscribes + notifies.

Either way, make sure you implement IDisposable and dispose of the StateSubscriber or unsubscribe from the events manually.