How do I access a variable in ForEach inside a Button view that's inside the ForEach view?

Code Block
@State private var menuItems = ["Heroes", "Items", "Tips"]
    var body: some View {
ZStack {
VStack {
ForEach(0 ..< menuItems.count) {_ in 
Button(action: {}) {
Text("\(menuItems[$index])")
.foregroundColor(Color.white)
}
.padding(.horizontal, 75.0)
.padding(.vertical, 25.0)
.background(Color.gray)
}
}
}

I get an "Cannot find '$index' in scope"
Accepted Answer

I get an "Cannot find '$index' in scope"

I cannot find any declarations of $index nor index in your code.

I guess you wanted to do something like this:
Code Block
@State private var menuItems = ["Heroes", "Items", "Tips"]
var body: some View {
ZStack {
VStack {
ForEach(0 ..< menuItems.count) {index in //<- Declare index here
Button(action: {}) {
Text("\(menuItems[index])")
.foregroundColor(Color.white)
}
.padding(.horizontal, 75.0)
.padding(.vertical, 25.0)
.background(Color.gray)
}
}
}
}


@OOPer, thanks. Lol. I can't believe I didn't notice that 😅
How do I access a variable in ForEach inside a Button view that's inside the ForEach view?
 
 
Q