Skip to content

Instantly share code, notes, and snippets.

@albeva
Created May 18, 2020 12:24
Show Gist options
  • Save albeva/3e9506c6cb94d9d8093c78f94fd8c560 to your computer and use it in GitHub Desktop.
Save albeva/3e9506c6cb94d9d8093c78f94fd8c560 to your computer and use it in GitHub Desktop.
import Foundation
import SwiftUI
import PlaygroundSupport
typealias OnClickHandler = (() -> Void)
struct ParentView: View {
@State var loveEnabled = false
var body: some View {
VStack {
ChildView1(loveEnabled: $loveEnabled)
Spacer()
ChildView2 {
// print("We love closures") print crashes my playgrounds
self.loveEnabled = true
}
}
}
}
struct ChildView1: View {
@Binding var loveEnabled: Bool
var message: String {
if loveEnabled {
return "But in a platonic way"
} else {
return "We love closures"
}
}
var body: some View {
Text(message)
}
}
struct ChildView2: View {
let callback: OnClickHandler
var body: some View {
Button(action: callback) {
Text("Tap me and then just hurt me")
}
}
}
PlaygroundPage.current.setLiveView(ParentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment