Popovers icon indicating copy to clipboard operation
Popovers copied to clipboard

Popover doesn't re-render its view when identity changes

Open raysarebest opened this issue 1 year ago • 2 comments

Let's say I have a custom-made text field that updates its content binding on every user keystroke. Let's also say I have a popover that displays some options based on what the user has typed into that text field:

let states = ["Alabama", "Alaska", ... ]

CustomTextField("US State", $selectedState)
    .popover(present: .constant(true)) {
        VStack {
            ForEach(states.map({ state in state.hasPrefix(selectedState) })) { state in
                Text(state)
            }
        }
    }

I would expect that the list of states displayed in the popover would update with every keystroke, but that doesn't seem to be the case. Strategically inserting debugger statements shows that the $selectedState is definitely getting updated for every keystroke, but the view that the popover renders never updates to include the narrowed list of possible states. How can I get it to update whenever its displayed view's identity changes so I can make something like this work?

raysarebest avatar Feb 17 '23 23:02 raysarebest