mrpmorris / Fluxor

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

Ho to avoid 'Selector has alread been set'? #252

Closed handcraftedsource closed 2 years ago

handcraftedsource commented 2 years ago

I used the new feature IStateSelector<TState, TValue>.

I have called Select in OnInitializedAsync of the Component on the injected property. The component is instantiated multiple times. If I switch from the first to the second component, I get an exception.

Is there any way to determine if selector is already set?

mrpmorris commented 2 years ago

OnInitializedAsync is being executed multiple times on the same object instance?

handcraftedsource commented 2 years ago

I have a small example here that can be used to reproduce the behavior. Just start the application and switch to the counter page.

https://github.com/handcraftedsource/selector-exception-sample

Another thing I noticed: it seems like I can't use IStateSelection alone, the component needs to have IState injected as well.

mrpmorris commented 2 years ago

AddScoped should have been AddTransient. https://github.com/mrpmorris/Fluxor/blob/252/PM-CanOnlyHaveOneStateSelectorPerComponent/Source/Fluxor/DependencyInjection/ServiceCollectionExtensions.cs#L46

I have added some other new things in there too, but need to write some unit tests when I am not busy. https://github.com/mrpmorris/Fluxor/blob/252/PM-CanOnlyHaveOneStateSelectorPerComponent/Docs/releases.md

mrpmorris commented 2 years ago

Could you try 5.1.0-Beta1 and let me know if it solves your problem?

handcraftedsource commented 2 years ago

Yes the problem seems to be fixed with this version. Thanks for the quick fix. 👍