2
2

More than 1 year has passed since last update.

【Swift】TCAで2つのActionを同時に実行する

Posted at

はじめに

TCAを使っていて同時にアクションを実行したい場面がありました。
やり方を知ったので記録しておきます。

実装

import SwiftUI
import ComposableArchitecture

struct ContentView: View {
    @ObservedObject private var viewStore: ViewStoreOf<Feature>
    
    let store: StoreOf<Feature>
    
    init(store: StoreOf<Feature>) {
        self.store = store
        self.viewStore = ViewStore(store, observe: { $0 })
    }

    var body: some View {
        Button {
            viewStore.send(.buttonTapped)
        } label: {
            Text("テスト")
        }
    }
}

struct Feature: Reducer {
    struct State: Equatable {}

    enum Action: Equatable {
        case buttonTapped
        case printA
        case printB
    }
    
    func reduce(into state: inout State, action: Action) -> Effect<Action> {
        switch action {
        case .buttonTapped:
+           return .concatenate(.send(.printA), .send(.printB))
        case .printA:
            print("A")
            return .none
        case .printB:
            print("B")
            return .none
        }
    }
}

おわり

TCAの知見ためていきたいです

2
2
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
2
2