How can I run an action when a state changes?

后端 未结 7 2207
醉话见心
醉话见心 2020-12-05 22:25
enum SectionType: String, CaseIterable {
    case top = \"Top\"
    case best = \"Best\"
}

struct ContentView : View {
    @State private var selection: Int = 0

           


        
7条回答
  •  情歌与酒
    2020-12-05 23:03

    Here is another option if you have a component that updates a @Binding. Rather than doing this:

    Component(selectedValue: self.$item, ...)
    

    you can do this and have a little greater control:

    Component(selectedValue: Binding(
        get: { self.item },
        set: { (newValue) in
                  self.item = newValue
                  // now do whatever you need to do once this has changed
        }), ... )
    

    This way you get the benefits of the binding along with the detection of when the Component has changed the value.

提交回复
热议问题