Is it intended for onchange to work with let properties?

Hi all,

by chance, we introduced code similar to the following in the body of one of our views:

struct MyView: View {
    let myInt: Int

    var body: some View {
        EmptyView() // Empty body
            .onChange(of: myInt) { newValue in
                …
            }
    }
}

Notice that a simple let of type Int is used here, rather than State or something similar. While it works and onChange is actually triggered when views are rebuilt, I’d like to clarify a few things:

  • Is this behavior actually expected and reliable, or is it a gray area and can be changed in the future?

  • If it is expected, can this be made clearer in the documentation?

  • If it is a gray area, can the documentation be improved to address it?

Thanks!

I think it will be called on re-build if the identity of the view is the same, so SwiftUI considers them the ”same”

Is it intended for onchange to work with let properties?
 
 
Q