ForEachStore with Simple SwiftUI button Inside

Good day. I'm new to composable architecture and Im currently trying to make a list of cards by using ForEachStore because I want to pass the current state to the detail screen of the list and also trying to implement the pagination for the list with ForEachStore

  var body: some View {
    WithViewStore(self.store, observe: ViewState.init) { (viewStore: ViewStore<ViewState, PhotoListFeature.Action>) in
      NavigationStack {
        ScrollView {
          LazyVStack {
            ForEachStore( self.store.scope(
              state: \.photoList,
              action: PhotoListFeature.Action.photo(id:action:)
            )) { _ in
             
              Button {
                
              } label: {
                
              }.task {
                
              }

            }
            
          }
        }
        .refreshable {
          Task {
            viewStore.send(.refreshPhotos)
          }
        }
        .navigationDestination(
          store: self.store.scope(
            state: \.destination,
            action: PhotoListFeature.Action.destination
          ),
          state: /PhotoListFeature.Destination.State.showPhotoDetail,
          action: PhotoListFeature.Destination.Action.showPhotoDetail
        ) { store in
          PhotoDetailView(store: store)
        }
        .task {
          viewStore.send(.fetchPhotos)
        }
      }
    }
  }

but unfortunately, I'm getting these errors:

Hi @vlainvaldez, there are a number of problems in the code you posted. First, you are ignoring the argument of the trailing closure of ForEachStore:

)) { _ in

That argument is the store that encapsulates the runtime for that row, and is typically passed on to a row view.

Second, you are using $0 in the action closure of Button, but that closure doesn't take any arguments.

You would probably have better luck if you moved the Button to its own view with its own TCA feature. It could be called PhotoRow, and that's the view that can have the buttonTapped action and the .task action. Just a suggestion.

1 Like

Thank you for this suggestion. I appreciate it!

I created a separate tca feature for the button for it to live in its own space rather than putting them all inside the Foreach store. Thank you! @mbrandonw