Skip to content
Discussion options

You must be logged in to vote

Hello,
You can probably use SwiftUI's Environment to handle the propagation for you. You could probably do something like this:

public struct Theme: Equatable {}
enum ThemeKey: EnvironmentKey {
  static var defaultValue: Theme { Theme() }
}
extension EnvironmentValues {
   public var theme: Theme {
    get { self[ThemeKey.self] }
    set { self[ThemeKey.self] = newValue }
  }
}

In any view you want to use the theme, you can expose a theme value:

@Environment(\.theme) var theme
var body: some View {
  Text("Hello!")
    .background(theme.backgroundColor)
}

If var theme: Theme is a property storing your theme in your root state, and assuming it is Equatable, you can scope into this value an…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by BaochengHan-TomTom
Comment options

You must be logged in to vote
1 reply
@derekc00
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants