Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Created December 9, 2024 13:18
Show Gist options
  • Save mbrandonw/ecf1a4493b691fd751b558ce787f6aa5 to your computer and use it in GitHub Desktop.
Save mbrandonw/ecf1a4493b691fd751b558ce787f6aa5 to your computer and use it in GitHub Desktop.
import SwiftUI
class Counter {
var count = 0
}
extension EnvironmentValues {
@Entry var counter = Counter()
}
struct ContentView: View {
@Environment(\.counter) var counter
var body: some View {
Form {
Text("\(counter.count)")
Button("Increment") {
counter.count += 1
print(counter.count)
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment