I'm trying to better understand how the onReceive modifier behaves in SwiftUI, specifically how its subscription lifecycle relates to view updates.
Consider this example:
TextField("Name", text: $name)
.onReceive(Just(name)) { value in
print(value)
}
This closure runs every time name changes. A common explanation is that SwiftUI recomputes body, which creates a new Just(name) publisher each time.
However, this raises some questions for me about how onReceive actually works internally:
When SwiftUI recomputes body, is the onReceive modifier recreated and resubscribed?
Does SwiftUI automatically cancel the previous subscription when the view updates?