Skip to content

In some scenario you need to use WithPerceptionTracking inside the block. #20

@alfredcc

Description

@alfredcc

Description

import Perception
import SwiftUI

@Perceptible
class ColorModel {
    var color: Color = .clear
}

struct ContentView: View {
    
    @State var model = ColorModel()
    
    var body: some View {
        let _ = print("\(Self.self): tracked change.")
        WithPerceptionTracking {
            VStack {
                ForEach(0 ... 3, id: \.self) { _ in
//                    WithPerceptionTracking {
                        model.color
//                    }
                }
            }
        }
    }
}


#Preview {
    ContentView()
}

If I comment out the above content, I will get the following runtime warning:
Perceptible state was accessed but is not being tracked. Track changes to state by wrapping your view in a 'WithPerceptionTracking' view.

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

I expect i can put WithPerceptionTracking outside ForEach

Actual behavior

We must put WithPerceptionTracking in each loop

Steps to reproduce

No response

Perception version information

No response

Destination operating system

No response

Xcode version information

No response

Swift Compiler version information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions