This is my code and "print("run to onReceive (text)")" run twice when text change (like a image). Why? and thank you!
import SwiftUI
class ContentViewViewModel : ObservableObject {
@Published var text = ""
}
struct ContentView: View {
@StateObject private var viewModel = ContentViewViewModel()
var body: some View {
ZStack {
TextField("pla", text: $viewModel.text)
.padding()
}
.onReceive(viewModel.$text) { text in
print("run to onReceive (text)")
}
}
}
2
Answers
Because you have a
@Published
variable inside the@StateObject
of your view, the changes in that variable will automatically update the view.If you add the
.onReceive()
method, you will:@Published
var.onReceive()
method listens to the changeJust delete the
.onReceive()
completely and it will work:I think it’s because the view is automatically updated as your
@Published
property in your ViewModel changes and the.onReceive
modifier updates the view yet again due to the 2 way binding created byviewModel.$text
resulting in the view being updated twice each time.If you want to print the text as it changes you can use the
.onChange
modifier instead.onChanged in SwiftUI