Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,20 @@ public struct ForEachStore<
where
Data == IdentifiedArray<ID, EachState>,
Content == WithViewStore<
OrderedSet<ID>, (ID, EachAction), ForEach<OrderedSet<ID>, ID, EachContent>
IdentifiedArray<ID, EachState>, (ID, EachAction),
ForEach<IdentifiedArray<ID, EachState>, ID, EachContent>
>
{
self.data = store.state.value
self.content = WithViewStore(
store,
observe: { $0.ids },
removeDuplicates: areOrderedSetsDuplicates
observe: { $0 },
removeDuplicates: { areOrderedSetsDuplicates($0.ids, $1.ids) }
) { viewStore in
ForEach(viewStore.state, id: \.self) { id -> EachContent in
// NB: We cache elements here to avoid a potential crash where SwiftUI may re-evaluate
// views for elements no longer in the collection.
//
// Feedback filed: https://gist.github.com/stephencelis/cdf85ae8dab437adc998fb0204ed9a6b
var element = store.state.value[id: id]!
return content(
ForEach(viewStore.state, id: viewStore.state.id) { element in
var element = element
let id = element[keyPath: viewStore.state.id]
content(
store.scope(
state: {
element = $0[id: id] ?? element
Expand Down