mrpmorris / Fluxor

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

Redux Dev Tools history not working when using public constructor in Action class #270

Closed bertolaz closed 2 years ago

bertolaz commented 2 years ago

Hi, I'm experiencing the following issue when using Redux Dev Tools. If I press the "Go Back" button to undo my actions, the State of my component resets directly to its initial state. Example: Starting from the initial state (ClickCount = 0) I click several times (eg .7) the IncrementCount Button. ClickCount now is 7. If I press once the "Go Back" button on the Redux Dev Tools, ClickCount is now 0.

This is the configuration of my CounterState

[FeatureState]
public class CounterState
{
  public int ClickCount { get; }

  public CounterState() {} // Required for creating initial state --> THIS DOES NOT WORK

  public CounterState(int clickCount)
  {
    ClickCount = clickCount;
  }
}

2022-03-08 16-29-16(1)

However, if if the Constructor that is required for creating the inital state is marked as "private" (as shown in the Tutorial) everything works as expected.

The tutorial states the following: "A parameterless constructor is required on state for determining the initial state, and can be private or public."

[FeatureState]
public class CounterState
{
  public int ClickCount { get; }

  private CounterState() {} // Required for creating initial state --> THIS WORKS

  public CounterState(int clickCount)
  {
    ClickCount = clickCount;
  }
}
mrpmorris commented 2 years ago

Ah yes, how unexpected :)

It looks like the serializer will use the parameterless constructor if it is public and the parameterised one if it is private. If you want the parameterless one to be public then decorate the parameterised one with [JsonConstructor]