Lite Redux is a simple library that implements state management in the Redux-style
You should install LiteRedux with NuGet:
Install-Package LiteRedux
- Register a dependency
services.AddLiteRedux(typeof(CounterState).Assembly);
- Add state
public class CounterState
{
public CounterState()
{
Value = 0;
}
public CounterState(int value)
{
Value = value;
}
public int Value { get; }
}
services.AddLiteRedux(typeof(CounterState).Assembly).AddState<CounterState>();
- Add action and reducer
public class IncrementAction
{
public IncrementAction() { }
}
public class IncrementActionReducer : Reducer<CounterState, IncrementAction>
{
protected override CounterState Reduce(CounterState state, IncrementAction action)
{
return new CounterState(state.Value + 1);
}
}
Inherit ReduxComponent
to access the state and store.
@inherits ReduxComponent<CounterState>
- Getting a state
@inherits ReduxComponent<CounterState>
<h1>Counter</h1>
<p>Current count: @State.Value.Value</p>
- Dispatching actions
private void IncrementCount()
{
Store.Dispatch(new IncrementAction());
}
To perform asynchronous operations, such as loading from a database, you can use Trigger
public class FetchTrigger : Trigger<FetchAction>
{
private readonly WeatherForecastService weatherForecastService;
public FetchTrigger(WeatherForecastService weatherForecastService)
{
this.weatherForecastService = weatherForecastService;
}
protected async override Task HandleAsync(FetchAction action, IStore store)
{
WeatherForecast[] data = await weatherForecastService.GetForecastAsync(action.Date);
store.Dispatch(new CompleteFetchAction(data));
}
}