Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active April 17, 2020 06:38
Show Gist options
  • Select an option

  • Save mbrandonw/d64d7238eafc5895d48c0d099b114e02 to your computer and use it in GitHub Desktop.

Select an option

Save mbrandonw/d64d7238eafc5895d48c0d099b114e02 to your computer and use it in GitHub Desktop.
/*
I expect the following to be printed from running this playground
and drilling down to the child view:
nextIsActive true
childIsActive false
------------
nextIsActive true
childIsActive true
But instead I get:
nextIsActive true
childIsActive false
------------
nextIsActive true
childIsActive true
------------
nextIsActive false
childIsActive true
------------
Notice that both booleans switch to true, but then nextIsActive
switches to false.
*/
import PlaygroundSupport
import SwiftUI
class AppState: ObservableObject {
@Published var nextIsActive = false {
didSet {
print("nextIsActive", nextIsActive)
print("childIsActive", childIsActive)
print("------------")
}
}
@Published var childIsActive = false {
didSet {
print("nextIsActive", nextIsActive)
print("childIsActive", childIsActive)
print("------------")
}
}
}
struct GrandView: View {
@ObservedObject var state = AppState()
var body: some View {
NavigationView {
VStack {
Text("Grand")
NavigationLink(
destination: NextView(state: self.state),
isActive: self.$state.nextIsActive
) {
Text("go to next view")
}
}
}
}
}
struct NextView: View {
@ObservedObject var state: AppState
var body: some View {
VStack {
Text("Next")
NavigationLink(
destination: Text("Child"),
isActive: self.$state.childIsActive
) {
Text("go to child view")
}
}
}
}
PlaygroundPage.current.setLiveView(GrandView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment