TuracBey
TuracBey

Reputation: 270

Presenting a Sheet modally, @State value is not changing

I have tried much time on it but I couldn't figure out why it is not working.

The problem is when I tap the button, the new value inside the sheet is not updated. It always show the same value which is set up in start.

 @State var value:String = "empty"
 @State var explorePageIsEnabled:Bool = false
    
   VStack{
    
    Button("tap me"){
        value = "the new one"
        exploreStatusIsEnabled.toggle()
    }
    .sheet(isPresented: $exploreStatusIsEnabled, content: {
        Text(value)
    })
    
}

Deployment target is IOS 14+

Upvotes: 4

Views: 760

Answers (1)

Raja Kishan
Raja Kishan

Reputation: 18914

Create a separate struct view for text and use Binding.

struct SheetView: View {
    @Binding var value: String
    
    var body: some View {
        Text(value)
    }
}

struct ContentView: View {
    
    @State private var value: String = "empty"
    @State private var explorePageIsEnabled: Bool = false
    
    var body: some View {
        VStack{
            
            Button("tap me"){
                value = "the new one"
                explorePageIsEnabled.toggle()
            }
            .sheet(isPresented: $explorePageIsEnabled, content: {
                SheetView(value: $value)
            })
        }
    }
}

Upvotes: 3

Related Questions