How to check for last item in a ForEachStore?

How to check if an item is the last item in userState? I need to hide a separator :man_facepalming:

List {
      state: \.userState,
      action: TeamAction.user(index:action:)
    ), content: UserView.init(store:)

The easiest one I can think of is var isLast: Bool property inside the state.

1 Like

I thought maybe there's a way to access the array that the ForEachStore is iterating over, and check the index of current item. Maybe it's possible to write an extension? It shouldn't add much performance overhead honestly.

You can access a ForEachStore's array by drilling down to the elements of the IdentifiedArray it is based on.

struct MyElement: Identifiable {
    let id: UUID

struct MyState: Equatable {
    let myArray: IdentifiedArrayOf<MyElement>

let lastElement = viewStore.myArray.elements.last 

Just be mindful that this returns an optional.

Terms of Service

Privacy Policy

Cookie Policy