一、State
1  | struct CounterState: Equatable {  | 
二、Action
1  | enum CounterAction: Equatable {  | 
三、Environment
1  | struct CounterEnvironment {}  | 
四、Reducer
1  | 
- 处理事件以及状态的变化
 - 返回Effect
 
1. combine
把多个reducer组合成一个
- reducers的顺序会影响结果
 
2. pullback
利用local state, action and environment构造全局的reducer
参数:
- toLocalState: 可写的keyPath,能够从
GlobalState中取出LocalState - toLocalAction: casePath,能将
LocalActioinextract/embed 到GlobalAction. - toLocalEnvironment: 函数,能将 
GlobalEnvironment转为LocalEnvironment. 
- toLocalState: 可写的keyPath,能够从
 使用:
// Global domain that holds a local domain: struct AppState { var settings: SettingsState, /* rest of state */ } enum AppAction { case settings(SettingsAction), /* other actions */ } struct AppEnvironment { var settings: SettingsEnvironment, /* rest of dependencies */ } // A reducer that works on the local domain: let settingsReducer = Reducer<SettingsState, SettingsAction, SettingsEnvironment> { ... } // Pullback the settings reducer so that it works on all of the app domain: let appReducer: Reducer<AppState, AppAction, AppEnvironment> = .combine( settingsReducer.pullback( state: \.settings, action: /AppAction.settings, environment: { $0.settings } ), /* other reducers */ ) <!--4-->
3. optional
将reducer中 state映射为State?
五、Store
1  | Store<State, Action>  | 
- 存储了state和action
 
1. WithViewStore
可以接受Store,并将其转换为@ObservedObject ViewStore<State, Action>
1  | var body: some View {  | 
1.1 ViewStore
1  | 
  | 
viewStore可以访问到state的属性
原理:将ViewStore声明为dynamicMemberLookup,并实现他的
subscript<LocalState>(dynamicMember keyPath: KeyPath<State, LocalState>) -> LocalState方法,即可访问到ViewStore中未定义的成员(若访问到成员不存在,就会返回subscript方法的执行结果)
send
viewStore可以发送一个action到store,store中的reducer会接受并处理
ViewStore不是线程安全的,因此send方法一定要在主线程调用
binding
- 初始化时传入get函数(获取state)和set函数(发送action)
 - 有关联值的枚举,如果只写关联值以外的部分,可以当做函数来使用:比如 
viewStore.binding(get: { $0.text }, send: BindingBasicsAction.textChange)中BindingBasicsAction.textChange就是一个输入为 String, 输出为 BindingBasicsAction 实例的函数 
2. scope
通过全局的Store获取local Store中的state或者action
- 参数
- toLocalState:修改local state
 - fromLocalAction:接收local action
 
 
1  | // Application state made from local states.  | 
3. IfLetStore
判断当前store中的State是不是可选的