title: How can I navigate to a detail view of an item by using an @EnvironmentObject to route the views?
body: <p>I have the following code in SwiftUI. I am expecting it to navigate from the list view to the PetView() with the proper name showing when tapping on one of the items in the ForEach loop or the button that says &quot;Go to first pet&quot;. However, when I tap on an item or the button, the app doesn't do anything. What am I doing wrong? Thank you for your help!</p>
  <pre><code>
  
  import SwiftUI
  
  @main
  struct TestListAppApp: App {
      var body: some Scene {
          WindowGroup {
              ContentView().environmentObject(ViewRouter())
          }
      }
  }
  
  </code></pre>
  <pre><code>
  
  import SwiftUI
  
  struct ContentView: View {
      @EnvironmentObject var viewRouter: ViewRouter
  
      
      var body: some View {
              
              ForEach(viewRouter.pets) { pet in
                  NavigationLink(
                      destination: PetView(),
                      tag: pet,
                      selection: $viewRouter.selectedPet,
                      label: {
                          Text(pet.name)
                      }
                  )
              }
              
              Button(&quot;Go to first pet.&quot;) {
                  viewRouter.selectedPet = viewRouter.pets[0]
              }
      
          
  }
      
  }
  </code></pre>
  <pre><code>
  
  import Foundation
  
  class ViewRouter: ObservableObject {
      @Published var selectedPet: Pet? = nil
      @Published var pets: [Pet] = [Pet(name: &quot;Louie&quot;), Pet(name: &quot;Fred&quot;), Pet(name: &quot;Stanley&quot;)]
  }
  
  </code></pre>
  <pre><code>
  
  import SwiftUI
  
  struct PetView: View {
      
      @EnvironmentObject var viewRouter: ViewRouter
  
      var body: some View {
          Text(viewRouter.selectedPet!.name)
      }
  }
  
  </code></pre>
  <pre><code>import Foundation
  
  struct Pet: Identifiable, Hashable {
      var name: String
      var id: String { name }
  
  }
  
  </code></pre>