How to manage ForEachStore with LazyVGrid?

Hi guys,

I'm trying to make work together a ForEachStore (to iterate the grid cells items stores) inside a LazyVGrid.

But I don't know if the LazyGrid feature is to work with the composable framework because it throws an error when running (and the preview is not working too)


WithViewStore(self.store) { viewStore in
            LazyVGrid(columns: columns, spacing: 0) {
                ForEachStore(
                    self.store.scope(state: \.items, action: AppAction.cellItem(index:action:)),
                    content: CellView.init(store:)
                )
            }
        }

Hi @brunobasas, thanks for letting us know that there might be potential trouble with our ForEachStore and the new lazy views in SwiftUI. We haven't had too much time to play with lazy views, so good to catch this early!

Can you share a bit more code so that we can reproduce the error locally? I was able to get a very simple LazyVStack working with the following code:

import ComposableArchitecture

struct Row: Equatable, Identifiable {
  let id = UUID()
}

struct LazyView: View {
  let store = Store<[Row], Never>(
    initialState: (1...1_000).map { _ in Row() },
    reducer: .empty,
    environment: ()
  )

  var body: some View {
    ScrollView {
      LazyVStack {
        ForEachStore(
          self.store.scope(state: { $0 }, action: { _ in fatalError() }),
          content: RowView.init(store:)
        )
      }
    }
  }
}

struct RowView: View {
  let store: Store<Row, Never>

  var body: some View {
    WithViewStore(self.store) { viewStore in
      Text("Hi \(viewStore.id)")
    }
  }
}

Is there anything you are doing in your code that is substantially different from what is above?

Thanks Brandon!

I don't know why my code was not working at all :exploding_head:. I tried your lines mixed with my code and it works now.

If I found the issue I will post it here.