LoginSignup
0
1

More than 3 years have passed since last update.

ReSwiftで複数のReducerってどうするん?

Last updated at Posted at 2020-04-03

はじめに

ReSwiftの勉強をしていて、Storeに登録するReducerが複数の時どうするんだっけ?と思ったのでメモです。

CounterExample

ReSwiftの公式の「CounterExample」を見ていて、以下のコードがありました。

AppDelegate.swift
import UIKit
import ReSwift

// The global application store, which is responsible for managing the appliction state.
let mainStore = Store<AppState>(
    reducer: counterReducer, //<---複数の時どうするの?
    state: nil
)

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

Reducerを複数登録する時ってどうすればいいんだろう?と疑問に思いました。
調べてみたら、Issuesにありましたね。

Is_it_possible_to_specify_an_array_of_reducers?#340
func combineReducers<T>(_ first: @escaping Reducer<T>, _ remainder: Reducer<T>...) -> Reducer<T> {
    return { action, state in
        let firstResult = first(action, state)
        let result = remainder.reduce(firstResult) { result, reducer in
            return reducer(action, result)
        }
        return result
    }
}

let reducer = combineReducers(first, second, third) // <--- これを使う!

stateやらreducerやらもう少し細かく分けると、自作しないといけないような気がしますが、とりあえずこいつを使っていくことにします。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1