When using .navigationDestination(isPresented:), the navigation hangs when in the following situation:
@State private var navigateToChatView = false
var body: some View {
MyView()
.navigationTitle("Users")
.searchable(text: $viewModel.searchText)
.navigationDestination(isPresented: $navigateToChatView) {
if let selectedUser = selectedUsers.first, let name = selectedUser.name {
NewComposeView(sendMessage: { message in
viewModel.sendMessage(message: message) // removing this works
})
.navigationTitle(name)
}
}
}
Just commenting out the viewModel function allows it to navigate to the view as expected. Not sure why this is an issue.
Also note that even making sendMessage a simple function that prints a statement is an issue so I don't think it has anything to do with sendMessage being an asynchronous call
Here is the NewComposeView code:
import SwiftUI
struct NewComposeView: View {
@State private var message: String = ""
var sendMessage: (String) -> Void
var body: some View {
HStack {
ZStack {
RoundedRectangle(cornerRadius: 18)
.stroke()
HStack {
TextField("Write a message", text: $message)
Spacer()
Button {
sendMessage(message)
message = "" // Clear the text field
} label: {
Image(systemName: "paperplane.fill")
.resizable()
.frame(width: 20, height: 20)
.font(.title)
}
}
.padding(EdgeInsets(top: 3, leading: 20, bottom: 3, trailing: 20))
}
.frame(height: 40)
}
.foregroundColor(Color(.white))
}
}