SwiftUI Previews

While building your app in SwiftUI, you'll want to use previews to quickly iterate on Views without having to build and run. If any of your Views need to access Parra's environment values or other APIs, you can wrap your previews in ParraAppPreview to inject the necessary configuration to support basic functionality and state to be rendered.

ParraAppPreview can optionally be provided with a custom Parra config object and auth state, so that you can create different previews for different situations. It is recommended that you use either ParraAuthState.authenticatedPreview or ParraAuthState.unauthenticatedPreview as your auth state since these inject a reasonable user info stub, but you're welcome to pass any auth state that you'd like.

import SwiftUI
import Parra

struct ContentView: View {
    @Environment(\.parraAuthState) private var parraAuthState

    var body: some View {
        if let userName = parraAuthState.user?.info.name {
            Text("Hello, \(userName)")
        } else {
            Text("Hello")
        }
    }
}

#Preview {
    ParraAppPreview(
        configuration: .default,
        authState: .authenticatedPreview
    ) {
        ContentView()
    }
}

Was this page helpful?