NavigationLInk

I have an array with 10 different categories. So I create NavigationLink for each of them, calling MyListView and passing data about the category.

NavigationView {
  List(categories) { category in
     NavigationLink(destination:
            MyListView(category: category)
     )          
     {
             CategoryRow(category: category)
     }
  }
}

So when I click in one of these 10 categories, I will call MyListView. Inside MyListView I have a call to the API bringing a lot of information about the chosen category.

The problem here is, even before clicking in any category, MyListView is being called and the API is called for all the 10 categories.

I only want to call MyListView and the API inside it, after selecting the category.

Whats the problem here ?

Thx

MyListView is being called

I do not understand what you mean by being called, but I guess you may be doing something in init or body, which may be executed at any time (which are hard to predict) by the runtime of SwiftUI.

If you want to do something on appearance of a view, you may need to use onAppear {...}:

(Or task {...} if you can target your app to iOS 15+.)

struct MyListView: View {
    let category: Category
    
    var body: some View {
        listViewContent()
            .onAppear {
                print("onAppear called")
                //Do API calls here...
            }
    }
    
    @ViewBuilder
    private func listViewContent() -> some View {
        Text("listViewContent...")
        //...
    }
}

You're right. I am doing in init. But if I remove from init, how can I create the loop to create the navigation links in body?

struct MyListView: View {
  var category: categoryModel
  @ObservedObject var store = MyStore()

  init(category: CategoryModel) {
    self.category = category
    store.getItemsByCategoryId: category.id)
}

   var body: some View {
     List(store.categories.indices, id:\.self) { categoryIndex in
            let category = store.categories[categoryIndex]

            NavigationLink(destination:
                CategoryView(categoryId: category.id)
            )

            {
                CategoryRow(category: category)
            }

          }
}

how can I create the loop to create the navigation links in body?

Have you read my answer?

you may need to use onAppear {...}

If you cannot apply generic code example into your actual code, please include enough code in your opening post.

(Please show an accurate code, no typo in it please.)

import SwiftUI

struct MyListView: View {
    let category: CategoryModel
    @ObservedObject var store = MyStore()  // <- I guess you should use singleton for `MyStore`, but you have not shown its detail...
    
    var body: some View {
        List(store.categories.indices, id:\.self) { categoryIndex in
            let category = store.categories[categoryIndex]
            
            NavigationLink(destination:
                            CategoryView(categoryId: category.id)
            ) {
                CategoryRow(category: category)
            }
            
        }
        .onAppear {
            self.store.getData(categoryId: category.id)
        }
    }
}
NavigationLInk
 
 
Q