How can I get selected item in Menu Picker SwiftUI

Im trying to get sort id which is come from my server . when user select sort in menu I should have selected id . But somehow when I selected sort its not triggered.Where do I make mistake ? when I change to selection to UUID then I can get uuıd for every selection but I should get their id which they are string that's why selection is string.


` @State private var selection = ""

    @Binding var sortClicked : Bool

    @ObservedObject var productListViewModel = ProductListViewModel()

    @State var sortListArray : [ProductListSortAndFilterList]

    

    

    var body : some View {

        

        HStack(alignment: .center){

            

            Spacer()

            

            Picker(selection: $selection, label: SortView().frame(height:UIScreen.main.bounds.height * 0.050), content: {

                ForEach(sortListArray,id:\.id) { item in

                    

                    if item.id == "sort" {

                        

                        ForEach(item.sortList ?? [],id:\.id) { data in

                            

                            Text(data.name ?? "")

                                .tag(data.id)

                             

                            

                        }

                        .onReceive(Just(selection)) { (value) in

                            

                            print(value)

                            

                        }

                        

                    }

                }

            })

            .pickerStyle(MenuPickerStyle())

            .padding()

            .background(Color(.systemBackground).edgesIgnoringSafeArea(.all))

            .cornerRadius(5)

            
```

Hi,
When you want to perform an action when the selection changes, You should use:

Code Block
.onChange(of: selection, perform: {value in
print(value)
})

And if your data in the ForEach already complies to Identifiable, you don't need to specify an id.
I tried that but still its not working .
How can I get selected item in Menu Picker SwiftUI
 
 
Q