所以我想在我的 blazor 服务器端应用程序中实现状态管理。我的目标是在网格中进行选择后调用一个动作。然后应将网格的值添加到状态中。我现在的问题是,我如何在州获得一些东西?该示例仅显示了如何增加计数,但我如何将应用程序中的数据获取到 reducer 或 action 中?
问问题
522 次
1 回答
1
当您调度一个动作时,您可以在该动作的构造函数中创建参数并传递您想要的数据。
Dispatcher.Dispatch(new FooAction(someData));
哪里FooAction
可以像
public class FooAction
{
public object SomeData { get; set; }
public FooAction(object someData)
{
SomeData = someData;
}
}
而在reducer中,你可以从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();
}
或者,使用替代减速器模式
public static class ReducersOrAnyOtherNameItDoesntMatter
{
[ReducerMethod]
public static MyState Reduce(MyState state, IncrementAction action) =>
new MyState(state.Counter += action.AmountToAddToCounter);
}
我不确定这是否是你想要的,你的问题不是很清楚,但这是一种“将数据从我的应用程序获取到减速器或动作”的方法。
于 2020-08-10T14:37:37.103 回答