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

navViewModel not persisted #33

Closed
danielbuechele opened this issue Dec 6, 2020 · 2 comments
Closed

navViewModel not persisted #33

danielbuechele opened this issue Dec 6, 2020 · 2 comments

Comments

@danielbuechele
Copy link

This might be due to my limited knowledge of SwiftUI or a flaw in my app. However, it happens that NavigationStackView is re-initialized for rerendering and because navViewModel is created in the constructor, it forgets its state.

Marking it is a @StateObject fixes the problem for me, but this method is only available starting iOS 14, so I'm not sure if that's what you want for this library.

And as I mentioned, perhaps NavigationStackView shouldn't be reinitialized at all, but that seems to be a problem with either SwiftUI or my app.

@danielbuechele
Copy link
Author

What also solves the problem for me is to wrap NavigationStackView in its own component:

public struct Navigation<Content: View>: View {
    let content: Content

    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }
    
    public var body: some View {
        NavigationStackView {
            content
        }
    }
}

@matteopuc
Copy link
Owner

Hi @danielbuechele, sorry for the late response. In order for me to help you understanding why the NavigationStackView gets recreated I'd need to take a look at your code (at least a part of your code that can reproduce the issue). Just a question: where do you create your NavigationStackView? Usually you want to create it as the very first view of your app.

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

2 participants