I have a SwiftUI view and I want to show an alert which displays additional information, however the issue is that when the alert is dismissed, it refreshes the whole view and navigates the user back up to the root view (because the state variable showFutherInfo changes when the alert is dismissed). I don't want this to happen.
A VERY simplified version of code looks like this:
struct PledgesView: View {
    @State var showFurtherInfo :Bool = false
    var body: some View {
        VStack{
                        Button(action: {
                            
                            showFurtherInfo = true
                              }) {
                            Image(systemName: "figure.walk").renderingMode(.original)
                                .resizable()
                                .frame(width: 50, height: 50, alignment: .center)
                               
                        }
                        .alert(isPresented:$showFurtherInfo) {
                            Alert(
                                title: Text("Important message"),
                                message: Text("Important Info"),
                                dismissButton: .default(Text("Dismiss"))
                            )
                        }
}
             
I want to stop this refreshing behaviour on alert, please. Basically I just need help decoupling the variable which controls the alert being shown, from this view
To avoid refreshing you have to move everything (you don't want to be refreshed) into separated view. Rendering engine will see that the view does not depend on modified state and will not re-render it.
Ie. it would be like
OtherView()     // << move everything inside !!
 .alert(isPresented:$showFurtherInfo) {
    Alert(
        title: Text("Important message"),
        message: Text("Important Info"),
        dismissButton: .default(Text("Dismiss"))
    )
 }
Note: the OtherView (named here) will be re-created anyway, because it is just called in computed property, but its body will not be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With