Sibling ForEach blocks with the same IDs share Compose keys since #533

Open
#563 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin, swift
Domain
mobile

Research direction

The bug is in how unrolled ForEach content gets Compose keys, so start from the key computation that uses currentCompositeKeyHash and the ForEach identity namespace, and from the rememberSaveable path that restores state under that key. Reproduce with two sibling ForEach blocks that share IDs, then check whether two Row(id: 1) views end up with the same key. Done means siblings get distinct keys and the TabView re-entry case still keeps its state. Pick the discriminator with a maintainer first, since the reporter's approach is still open.

Written by the indexing model from the issue text.

Description

Since #533, unrolled ForEach content is keyed by identity alone. That fixed the TabView remount, but two sibling ForEach blocks with the same IDs now get the same Compose keys:

struct Row: View {
    let list: String
    let id: Int
    @State var taps = 0
    var body: some View {
        Text("\(list) \(id): \(taps)").onTapGesture { taps += 1 }
    }
}

VStack {
    ForEach([1, 2], id: \.self) { Row(list: "favorites", id: $0) }
    ForEach([1, 2, 3], id: \.self) { Row(list: "all", id: $0) }
}

Both Row(id: 1) views get the same currentCompositeKeyHash, so rememberSaveable restores them under one key and a row can take the other block's state. In a Skip Fuse app (favorites and recents of the same items in one LazyVStack), this trapped in syncState on a null StateSupport after scrolling, on an Android 16 emulator with skip-ui 1.62.0.

Keying by (ForEachIdentityNamespace, identity), as before #533, separates siblings, but the namespace is remembered, so it changes on re-entry, which is the TabView case. A discriminator that survives re-entry, such as the ForEach's position among its parent's children combined with the identity, would fix both. Happy to open a PR once you've picked an approach.

Found with Claude Code while porting a SwiftUI app to Android with Skip Fuse.

Dominant language
Swift
Stars
334
Forks
95
Avg merge
2d 10h
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from skiptools/skip-ui

All issues in skiptools/skip-ui

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.