Skip to content
\n

Non-TCA version

\n
import SwiftUI\n\n// MARK: - List\n@MainActor class ListStoreNonTCA: ObservableObject {\n  struct State {\n    var cellStores: [CellStoreNonTCA] = []\n  }\n  @Published var state: State\n  init() {\n    self.state = State()\n  }\n  \n  func addTapped() {\n    let newId = state.cellStores.count\n    state.cellStores.append(.init(state: .init(id: newId)))\n  }\n}\n\nstruct ContentViewNonTCA: View {\n  @ObservedObject var store = ListStoreNonTCA()\n  \n  var body: some View {\n    List {\n      Button(\"Add row\") {\n        store.addTapped()\n      }\n\n      ForEach(store.state.cellStores, id: \\.state.id) { cellStore in\n        CellViewNonTCA(store: cellStore)\n      }\n    }\n    .listStyle(.plain)\n  }\n}\n\n// MARK: - Cell\n@MainActor final class CellStoreNonTCA: ObservableObject {\n  struct State {\n    let id: Int\n    var text: String? = nil\n  }\n  @Published var state: State\n  \n  init(state: State) {\n    self.state = state\n  }\n  \n  func onAppear() {\n    Task {\n      state.text = \"Content fetched at \\(Date())\"\n    }\n  }\n}\n\nstruct CellViewNonTCA: View {\n  @ObservedObject var store: CellStoreNonTCA\n\n  var body: some View {\n    let _ = print(\"rendering cell \\(store.state.id)\")\n\n    VStack {\n      Text(store.state.text ?? \"\")\n        .onAppear { store.onAppear() }\n    }\n  }\n}\n\n#Preview {\n  ContentViewNonTCA()\n}
\n

Thanks a lot!

","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

Hey @ba01ei , I believe your question relates to another one that was asked recently. You might find this thread helpful:
\n#2973

","upvoteCount":0,"url":"https://github.com/pointfreeco/swift-composable-architecture/discussions/3682#discussioncomment-13205438"}}}

How to get List to not re-render cells that didn't change #3682

Answered by kevinanderson7
ba01ei asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @ba01ei , I believe your question relates to another one that was asked recently. You might find this thread helpful:
#2973

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kevinanderson7
Comment options

Answer selected by ba01ei
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants