-
Notifications
You must be signed in to change notification settings - Fork 84
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
Comments
I'm facing the similar problem. In my case, ScrollView's scroll state is reset. |
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. |
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? |
Using a standard NavigationView and NavigationLink, the @State variables retain their values when pushed and subsequently popped back into view. iOS example:
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:
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?
The text was updated successfully, but these errors were encountered: