Add Callbacks To View Swiftui

[Solved] Add Callbacks To View Swiftui | Swift - Code Explorer | yomemimo.com
Question : add callbacks to view swiftui

Answered by : quaint-quail-rblyi6q3wthz

struct MyComponen: View { @Binding var alert: String var action: (() -> Void)? var body: some View { VStack { Text("Alert: \(alert)") if nil != action { Button(action: action!) { Text("Action") } } } }
}
extension MyComponen { func foo(perform action: @escaping () -> Void ) -> Self { var copy = self copy.action = action return copy }
}
struct TestCustomModifier: View { @State var message = "state 2" var body: some View { VStack { MyComponen(alert: .constant("state 1")) MyComponen(alert: $message).foo(perform: { print(">> got action") }) } }
}
struct TestCustomModifier_Previews: PreviewProvider { static var previews: some View { TestCustomModifier() }
}

Source : https://stackoverflow.com/questions/58780177/swiftui-how-add-custom-modifier-with-callback | Last Update : Sun, 31 Jan 21

Answers related to add callbacks to view swiftui

Code Explorer Popular Question For Swift