はじめに
おととい、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)
}
}
おわり
いろんな書き方があって難しい