Skip to content

Instantly share code, notes, and snippets.

@pitt500
Created September 17, 2022 22:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitt500/90e3ce770d8800d821ddb0b1c5660b13 to your computer and use it in GitHub Desktop.
Save pitt500/90e3ce770d8800d821ddb0b1c5660b13 to your computer and use it in GitHub Desktop.
More context about this demo here: https://youtu.be/SfFDj6qT-xg
import SwiftUI
import ComposableArchitecture
struct State: Equatable {
var counter = 0
}
enum Action: Equatable {
case increaseCounter
case decreaseCounter
}
struct Environment {
// Future Dependencies...
}
let reducer = Reducer<
State, Action, Environment
> { state, action, environment in
switch action {
case .increaseCounter:
state.counter += 1
return Effect.none
case .decreaseCounter:
state.counter -= 1
return Effect.none
}
}
struct ContentView: View {
let store: Store<State, Action>
var body: some View {
WithViewStore(self.store) { viewStore in
HStack {
Button {
viewStore.send(.decreaseCounter)
} label: {
Text("-")
.padding(10)
.background(.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.buttonStyle(.plain)
Text(viewStore.counter.description)
.padding(5)
Button {
viewStore.send(.increaseCounter)
} label: {
Text("+")
.padding(10)
.background(.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.buttonStyle(.plain)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(
store: Store(
initialState: State(),
reducer: reducer,
environment: Environment()
)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment