Skip to content

Latest commit

 

History

History
44 lines (39 loc) · 1.15 KB

Middleware.md

File metadata and controls

44 lines (39 loc) · 1.15 KB

Add middleware

  1. Add middleware builder function to MainFeature.swift
// ...

extension MainFeature {
    static func middleware() -> [Middleware<MainFeature.State, MainFeature.Action>] {
        return [
            createMiddleware({ dispatch, getState, action in
                switch action {
                case .decrement:
                    print("Decrement action is called")

                case .increment:
                    print("Increment action is called")
                }
            })
        ]
    }
}
  1. Add middleware to store initializer
final class MainModuleBuilder {
    func build() -> UIViewController {
        let store = Store<MainFeature.State, MainFeature.Action>(
            reducer: MainFeature.reducer(),
            state: MainFeature.State(),
            initialAction: nil,
            middleware: MainFeature.middleware()
        )

        let viewController = MainViewController(store: store)
        return viewController
    }
}
  1. Run app and tap on "plus" button. The next output will be printed to console
> Increment action is called

Next: Asyncronious events