Mario Bosak
Mario Bosak

Reputation: 21

Working with Blazor Fluxor for State Manangement

so I want to implement State Management into my blazor Server side app. My goal is to invoke an action after a selection in a grid. The Value of the Grid should then be added to the State. My Problem now is, how do I get something in the State ? The example only showed how to increment a count, but how would I get data from my application into the reducer or action ?

Upvotes: 2

Views: 1327

Answers (1)

Vencovsky
Vencovsky

Reputation: 31683

When you dispatch an action, you can create parameters in the constructor for that action and pass the data you want.

Dispatcher.Dispatch(new FooAction(someData));

where FooAction can be something like

public class FooAction 
{
    public object SomeData { get; set; }

    public FooAction(object someData)
    {
        SomeData = someData;
    }
}

And in the reducer, you can get the data from the action

public override BarState Reduce(BarState state, FooAction action)
{
    // access data from BarState with state.something
    // access data from FooAction with action.something
    var someData = action.SomeData;   

    // ...do whatever you want with the data

    return new BarState();
}

Or, using the alternative reducer pattern

public static class ReducersOrAnyOtherNameItDoesntMatter
{
  [ReducerMethod]
  public static MyState Reduce(MyState state, IncrementAction action) =>
    new MyState(state.Counter += action.AmountToAddToCounter);
}

I'm not sure if this is what you want, your question isn't that clear, but this is a way to "get data from my application into the reducer or action".

Upvotes: 1

Related Questions