Search code examples
c#blazorfluxor

How to deal with IEnumerables/Arrays/Collections in Fluxor State?


I'm currently trying to implement Fluxor for my Blazor WASM app and all the instructions/tutorials I found recommended something like this example for the Store:

public record AppStore {
   int ClickCounter,
   bool IsLoading,
   WeatherForecast[]? Forecasts
}

and then only talk about initial state and updates only happen to the bool and the int while the array is only ever replaced outright. I.e. the examples always fetch the complete data from the server, e.g. a 100 entries.

Now, here's my question: How do I properly deal with the array in my reducer when I have already 100 entries in there and only want do add/update/delete one? Is that even a good idea in the first place?


Solution

  • The best thing to do is to use ImmutableList<T> or ImmutableArray<T> instead, as this class is optimised for the purpose of returning a new instance that includes old data but without having to copy the elements.

    I've recently released a new library called Reducible that helps to create complex state reducers. It results in fewer updates (e.g. a new parent object isn't created if an item in the list is not replaced).

    https://github.com/mrpmorris/Reducible/blob/master/README.md