Menu Picker .onChange (doesn't work for the first selection)

This simple example demonstrates that the .onChange statement does not execute on the first selection, but does print the selection upon the second and later selections. Looking for an explanation and a fix if one exists.

import SwiftUI
// Xcode version 13.4.1 (13F100)

let myFruits: [String] = ["Apple","Banana","Grape","Peach"]

struct ContentView: View {
  @State var fruitName: String = "none"
  var body: some View {
    VStack {
      Menu {
        PickFruit(fruitName: $fruitName)
      } label: {Text("Select A Fruit")}
      Text(fruitName)
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

struct PickFruit: View {
  @Binding var fruitName: String
  var body: some View {
    VStack {
      Text(fruitName)
      Picker("Select", selection: $fruitName) {
        ForEach(myFruits, id: \.self) { myFruits in
          Text(myFruits)
        }
      }.onChange(of: fruitName) {newValue in print(newValue)}
    }
  }
}
Answered by MobileTen in 715726022

This example shows that it does print on the first selection:

import SwiftUI

struct ContentView: View {
    @State var fruitName: String = "none"
    var body: some View {
        VStack {
            Menu {
                PickFruit(fruitName: $fruitName)
            } label: {Text("Select A Fruit")}
            Text(fruitName)
        }
        .onChange(of: fruitName) { [fruitName] newValue in // move onchange to here
            print(newValue)
            print(fruitName)
        }
    }
}

struct PickFruit: View {
    let myFruits: [String] = ["Apple","Banana","Grape","Peach"]
    @Binding var fruitName: String
    var body: some View {
        VStack {
            Text(fruitName)
            Picker("Select", selection: $fruitName) {
                ForEach(myFruits, id: \.self) { myFruits in
                    Text(myFruits)
                }
            }
        }
    }
}
Accepted Answer

This example shows that it does print on the first selection:

import SwiftUI

struct ContentView: View {
    @State var fruitName: String = "none"
    var body: some View {
        VStack {
            Menu {
                PickFruit(fruitName: $fruitName)
            } label: {Text("Select A Fruit")}
            Text(fruitName)
        }
        .onChange(of: fruitName) { [fruitName] newValue in // move onchange to here
            print(newValue)
            print(fruitName)
        }
    }
}

struct PickFruit: View {
    let myFruits: [String] = ["Apple","Banana","Grape","Peach"]
    @Binding var fruitName: String
    var body: some View {
        VStack {
            Text(fruitName)
            Picker("Select", selection: $fruitName) {
                ForEach(myFruits, id: \.self) { myFruits in
                    Text(myFruits)
                }
            }
        }
    }
}

Nice fix for this problem. The purpose of this example was getting the .onChange to work correctly to call a function for selecting multiple values. Thanks for the help.

Menu Picker .onChange (doesn't work for the first selection)
 
 
Q