0
0

More than 1 year has passed since last update.

【TCA】ViewStateを使っている時に@ObservedObjectでViewStoreを使用する

Posted at

はじめに

おととい、2種類のViewStoreの扱い方を記事にしました。

Reducerはこんな感じ

struct Feature: Reducer {
    struct State: Equatable {
        var text = "Hello, World!"
    }
    
    enum Action: Equatable {
        
    }
    
    func reduce(into state: inout State, action: Action) -> Effect<Action> {
        
    }
}

実装

import SwiftUI
import ComposableArchitecture

struct ContentView: View {
    @ObservedObject private var viewStore: ViewStore<ViewState, Feature.Action>

    let store: StoreOf<Feature>
    
    init(store: StoreOf<Feature>) {
        self.store = store
        self.viewStore = .init(store, observe: ViewState.init(state:))
    }

    struct ViewState: Equatable {
        let text: String
        
        init(state: Feature.State) {
            self.text = state.text
        }
    }

    var body: some View {
        Text(viewStore.text)
    }
}

おわり

いろんな書き方があって難しい

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