前回ミニマムの構成で試してみたので、次は少し応用。
- Store内の構成をカテゴリー分け
- カテゴリー毎のボタンを押したらそれぞれに反映される
 Store内の構成をカテゴリー分け
 Store内の構成をカテゴリー分け
次のStateを追加
- CategoryAState
- CategoryBState
- CategoryCState
それぞれ内部にカウンター(Int)を持っている
CategoryAState.swift
import ReSwift
/// Category A
struct CategoryAState: StateType {
    var counter: Int?
}
AppStateを次のように変更
AppState.swift
struct AppState: StateType {
    var categoryA = CategoryAState()
    var categoryB = CategoryBState()
    var categoryC = CategoryCState()
}
それぞれのCategoryのReducerを追加
CategoryAReducer.swift
import ReSwift
extension CategoryAState {
    static func CategoryAReducer(action: Action, state: CategoryAState?) -> CategoryAState {
        let state = state ?? CategoryAState(counter: 0)
        var newState = state
        switch action {
        case _ as InputAction:
            newState = CategoryAState(counter: (action as! InputAction).value)
        default:
            break
        }
        return newState
    }
}
AppReducer.swiftを次のように修正
AppReducer.swift
import ReSwift
func AppReducer(action: Action, state: AppState?) -> AppState {
    return AppState(categoryA: CategoryAState.CategoryAReducer(action: action, state: state?.categoryA),
                    categoryB: CategoryBState.CategoryBReducer(action: action, state: state?.categoryB),
                    categoryC: CategoryCState.CategoryCReducer(action: action, state: state?.categoryC))
}



 
0 件のコメント:
コメントを投稿