Created
May 18, 2020 12:24
-
-
Save albeva/3e9506c6cb94d9d8093c78f94fd8c560 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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