Skip to content

TabView is stale in if ... else ... #8

@wchen258

Description

@wchen258

The following View when used as a View for a Tab, will keep using the stale View, and not updating

struct HomeViewWrapper: View {
    
    var body: some View {
            if let _ = Me.shared.user, let _ = Me.shared.token {
                HomeView()
            } else {
                LoginView()
            }
    }
}

However, the code will work, if the if else is wrapped in a ZStack i.e.

struct HomeViewWrapper: View {
    
    var body: some View {
            ZStack {
                    if let _ = Me.shared.user, let _ = Me.shared.token {
                           HomeView()
                     } else {  
                            LoginView()
                     }
            }
    }
}

The UI will update correctly.
To be more sure, the following code

struct HomeViewWrapper: View {
    
    @State var isLoggedIn: Bool = false
    
    var body: some View {
        Group {
            switch isLoggedIn {
            case true:
                let _ = print("Should see Home")
                HomeView()
            case false:
                LoginView()
            }
        }
        .onAppear {
            if let _ = Me.shared.user, let _ = Me.shared.token {
                isLoggedIn = true
                print("find token")
            } else {
                print("cannot find token")
            }
        }       
    }
}

Will print "find token" and "Should see Home" when onAppear, however the UI stuck at LoginView(). Considering the one without the ZStack always works when I using the native SwiftUI TabView, I would consider this is a bug :)))

BTW, CustomTabView is indeed highly customizable and easy to use. Thank you for the great project

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions