代码拉取完成,页面将自动刷新
Done Redux 是基于Redux思想的组合式状态管理框架,非常适合构建基于.NET7的应用程序,即Donet Redux。
它有四个优势:
- 函数式编程
- 可预测状态容器
- 组件化
- 灵活装配
using Redux;
var state = CounterState.initState();
var reducer = CounterReducer.buildReducer();
var store = StoreCreator.createStore<CounterState>(state, reducer);
store.Subscribe(() =>
{
var lastState = store.GetState();
var stateJson = System.Text.Json.JsonSerializer.Serialize(lastState);
Console.WriteLine($"[Subscribe] last-state:{stateJson}");
});
store.Dispatch(CounterActionCreator.add(1));
store.Dispatch(CounterActionCreator.minus(2));
internal class CounterReducer
{
internal static Reducer<CounterState> buildReducer()
{
var map = new Dictionary<Object, Reducer<CounterState>>();
map.Add(CounterAction.add, _add);
map.Add(CounterAction.minus, _minus);
return Converter.asReducers<CounterState>(map);
}
private static CounterState _minus(CounterState state, Redux.Action action)
{
CounterState? newState = state.Clone(); //clone
newState.Count -= action.Payload;
return newState;
}
private static CounterState _add(CounterState state, Redux.Action action)
{
CounterState? newState = state.Clone(); //clone
newState.Count += action.Payload;
return newState;
}
}
internal enum CounterAction
{
add,
minus,
}
internal class CounterActionCreator
{
internal static Redux.Action add(int payload)
{
return new Redux.Action(CounterAction.add, payload);
}
internal static Redux.Action minus(int payload)
{
return new Redux.Action(CounterAction.minus, payload);
}
}
[Serializable]
internal class CounterState
{
public int Count { get; set; } = 0;
public static CounterState initState()
{
var state = new CounterState();
return state;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。