Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@State variables get reset after pop #71

Closed
rjo opened this issue Nov 16, 2021 · 3 comments
Closed

@State variables get reset after pop #71

rjo opened this issue Nov 16, 2021 · 3 comments

Comments

@rjo
Copy link

rjo commented Nov 16, 2021

Using a standard NavigationView and NavigationLink, the @State variables retain their values when pushed and subsequently popped back into view. iOS example:

import SwiftUI


struct ContentView: View {
    var body: some View {
        NavigationView {
            AAA()
        }
    }
}

struct AAA: View {
    @State var text = "default text"
    var body: some View {
        VStack {
            TextEditor(text: $text)
            NavigationLink(destination: BBB()) {
                Text("Push")
            }
        }
    }
}

struct BBB: View {
    var body: some View {
        Text("BBB")
    }
}

Edit the text, click Push, then go Back. The texteditor retains the changes that were made before pushing.

I tried the same thing with StackNavigationView and find that my @State variable is reset after a pop.

iOS example:

import SwiftUI
import NavigationStack

struct ContentView: View {
    var body: some View {
        NavigationStackView {
            AAA().padding()
        }
    }
}

struct AAA : View {
    @State private var data = "default text"

    var body: some View {
        VStack {
            TextEditor(text: $data)
            PushView(destination: BBB()) {
                Text("Push")
            }
        }
    }
}

struct BBB : View {
    var body: some View {
        PopView {
            Text("Pop")
        }
    }
}

If you edit text the editor, hit Push, then Pop back you see the text has been reset to its default state.

Is this expected behavior?

@ben73
Copy link

ben73 commented Nov 17, 2021

I'm facing the similar problem. In my case, ScrollView's scroll state is reset.
Is there a solution?

@matteopuc
Copy link
Owner

Hi @rjo and @ben73, thanks for your question. In SwiftUI when you remove a view from its hierarchy the state is reset. This behaviour is outlined in the README of this project in the Issue section. In that section you can also find a workaround for the problem.

@rjo
Copy link
Author

rjo commented Nov 18, 2021

Thanks for pointing our that bit of readme @matteopuc, I had missed that.

However, I'm just really confused as to why NavigationView does not suffer this problem and @State is preserved between presentations. Do we conjecture that NavigationView has some kind of privileged insight into things that allows this behavior?

I've wondered whether NavigationView does not actually remove views from the hierarchy, like it translates them off screen or overlays child views in some way.

Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants