Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI @Binding not updating in child

Tags:

swift

swiftui

In my main view I have the following variable:

@State private var link = "https://www.google.com"

The view has two children that contain the variable's binding:

@Binding var text: String

, initialized like this: Child(text: $link)

Child 1 updates the variable, the parent receives the change, but Child 2 still keeps the first value

like image 497
Toma Avatar asked Nov 16 '19 23:11

Toma


Video Answer


1 Answers

If you want a view to rebuild itself, you need to modify its state, environment object or observed object; those are the only things the view subscribes to. If you just modify a binding the view has no way of knowing it needs to rebuild. You should have the binding modify as a side effect either the environment, state or observed object of the view. Since you have some shared state between children and a parent view and @EnvironmentObject seems like the right place to do it.

like image 62
Josh Homann Avatar answered Oct 19 '22 03:10

Josh Homann