Skip to content

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

License

Notifications You must be signed in to change notification settings

DocTi/blazor-lite-redux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages