pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.25k stars 1.42k forks source link

Tabview does not update when scoping in a foreach loop #2842

Closed Scocher closed 6 months ago

Scocher commented 7 months ago

Description

When running this code the updateIndex does not update. TabView(selection: $store.selectedID.sending(\.updateIndex)) { ForEach(Array(store.scope(state: \.rows, action: \.rows).enumerated()), id: \.element) { index, store in Text("\(store.id)").tag(store.id) .frame(maxWidth: .infinity, maxHeight: .infinity) .background((index % 2 == 0 ? Color.blue : Color.red).opacity(0.2)) } } .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always)) compared to this: TabView(selection: $store.selectedID.sending(\.updateIndex)) { ForEach(Array(store.rows.enumerated()), id: \.element) { index, item in Text("\(item.id)").tag(item.id) .frame(maxWidth: .infinity, maxHeight: .infinity) .background((index % 2 == 0 ? Color.blue : Color.red).opacity(0.2)) } } .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))

https://github.com/pointfreeco/swift-composable-architecture/assets/2694492/bb95a754-23fa-4dc1-ab75-63f9b752eca1

SimpleProject.zip

Checklist

Expected behavior

I expect the index to update.

Actual behavior

It does not.

Steps to reproduce

Run the project I have attached.

The Composable Architecture version information

'1.8.2', 'main'

Destination operating system

iOS 17.2

Xcode version information

15.2

Swift Compiler version information

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0
domagojstankovic commented 6 months ago

@Scocher seems like adding id: \.state.id to ForEach does the job, at least it worked for me.

Link to the original solution.

stephencelis commented 6 months ago

@domagojstankovic Notes the issue, which is that the identity should be based on the data in the store and not the identity of the store itself. I'm going to convert this to a discussion since it's not technically a bug with the library.