Replies: 2 comments 1 reply
-
Is this a BUG? 😭 |
Beta Was this translation helpful? Give feedback.
-
Hi @zhuscat, this is a SwiftUI bug and it has to do with views with escaping closures. @Observable
class MyFooStore {
var shouldShow = false
}
struct FooView: View {
@State var localShouldShow = false
@State var store = MyFooStore()
var body: some View {
GeometryReader { _ in
VStack {
if store.shouldShow && localShouldShow {
Text("SHOW")
}
Button {
store.shouldShow.toggle()
} label: {
Text("TOGGLE")
}
}
.onAppear {
localShouldShow = true
}
}
}
} The fix is to eagerly access Since this isn't an issue with the library I am going to convert it to a discussion. Please feel free to continue the conversation over there! |
Beta Was this translation helpful? Give feedback.
-
Description
Here is my code:
I modified the value of localShouldShow in onAppear, and then clicked the button to change the value of shouldShow in the store. However, after clicking, "SHOW" does not appear. I found that the value of localShouldShow is still false when the view is rendered.
But if I change the order of the conditional statement to
if localShouldShow && store.shouldShow
, it works normally. Is this a bug in Swift Perception?20241225-224511.mp4
Checklist
@Observable
macro or another tool from theObservation
framework, please file it directly with Apple.main
branch of this package.Expected behavior
View can update
Actual behavior
View not update
Steps to reproduce
use the code above
Perception version information
1.4.1
Destination operating system
Preview
Xcode version information
Version 16.1 (16B40)
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions