'How to display Alert dialog on MacOS using SwiftUI

How can I display an alert dialog box from a menu item for MacOS apps using SwiftUI? The usual code which works for iOS @State var isOn = false and .alert("title", isPresented: isOn) {..} doesn't work.

@main
struct MyApp: App {

var body: some Scene {
    WindowGroup {
        ContentView()
    }.commands {
        CommandMenu("Test menu") {
            Button(action: {
                // I want to show an alert dialog dialog here.
            }) {
                Text("Click Me")
            }
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source