Hi milutz,
I think that perhaps, because @State is not explicitly detected in any View, it never invalidates and refreshes. So, by adding .id() to TestView, it can force and tell that it is not the same View anymore.
Hoping that this really helps you,
struct ContentView: View {
@State private var number = 41
var body: some View {
VStack {
TestView(number: number)
.fixedSize()
}.id(number)
.task {
try! await Task.sleep(nanoseconds: 1_000_000_000)
await MainActor.run {
print("adjusting")
number = 42
}
}
}
}
////////////////////////
// MARK: - TestView -
public struct TestView: View {
public init(number: Int) {
self.number = number
__internalNumber = .init(initialValue: number)
print("Init number: \(self.number)")
print("Init _internalNumber: \(_internalNumber)")
}
var number: Int
@State private var _internalNumber: Int
public var body: some View {
VStack(alignment: .leading) {
Text("number: \(number)")
Text("internal: \(_internalNumber)")
}
.debugAction {
Self._printChanges()
print("number: \(number)")
print("_internalNumber: \(_internalNumber)")
}
}
}
// MARK: - debugAction
extension View {
func debugAction(_ closure: () -> Void) -> Self {
closure()
return self
}
}