Skip to content

Latest commit

 

History

History
96 lines (83 loc) · 1.99 KB

README.md

File metadata and controls

96 lines (83 loc) · 1.99 KB

Lite Redux

Lite Redux is a simple library that implements state management in the Redux-style

Installing

You should install LiteRedux with NuGet:

Install-Package LiteRedux

Getting started

Example project

  1. Register a dependency
services.AddLiteRedux(typeof(CounterState).Assembly);
  1. 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>();
  1. 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);
  }
}

Redux component

Inherit ReduxComponent to access the state and store.

@inherits ReduxComponent<CounterState>
  1. Getting a state
@inherits ReduxComponent<CounterState>

<h1>Counter</h1>

<p>Current count: @State.Value.Value</p>
  1. Dispatching actions
private void IncrementCount()
{
  Store.Dispatch(new IncrementAction());
}

Trigger

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));
  }
}