Created
March 18, 2022 19:32
-
-
Save Thonyvb/69dff923fc54ed123404b16e1b7e4600 to your computer and use it in GitHub Desktop.
SwiftUI Property Wrapper Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ProductView: View { | |
@State private var isFavorite: Bool = false | |
var body: some View { | |
// ... | |
FavButton(isFavorite: $isFavorite) // Pass a binding. | |
} | |
} | |
struct FavButton: View { | |
@Binding var isFavorite: Bool | |
var body: some View { | |
Button(action: { | |
isFavorite.toggle() | |
}, label: { | |
Image(isFavorite ? "Filled_Heart" : "Heart") | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment