Doesn't onAppear get called after initializing?

Viewed 443

I'm curious why ContView displays

because, isn't onAppear called after initializing ContView? Shouldn't filteredCategories still be [String]() and modifiedTitle be ""?

struct Options {
    var categories = [String]()
    var title = "Title"
}
struct ParentView: View {
    var options = Options(categories: ["123", "asd", "string"], title: "Some title")
    
    @State var filteredCategories = [String]() /// @State so that I can change the value later
    @State var modifiedTitle = ""
    
    var body: some View {
        ContView(categories: filteredCategories, title: modifiedTitle) /// initializing ContView
        .onAppear { /// called after?
                
            var filteredCategories = [String]()
            for category in options.categories { 
                if category.contains("s") { /// this keeps only "asd" and "string"
                    filteredCategories.append(category)
                }
            }
            self.filteredCategories = filteredCategories
            self.modifiedTitle = options.title
        }
    }
}
struct ContView: View {
    var categories: [String] = [String]()
    var title: String = "Support"
    
    var body: some View {
        VStack {
            Text("categories: \(categories.count)")
            Text("title: \(title)")
        }
    }
}
0 Answers
Related