TCA stores and SwiftUI's environmentObject

Looking though TCA example code, stores are passed though the initializers of child views until they they are eventually utilized - as opposed adding stores to the root view's environment, then retrieving them from the environment at the point of use.

Is this to keep sample code as straight forward and implicit as possible or are there pitfalls with the use of environmentObject when writing tests or with TCA in general?

Thanks,

Scott

It would seem like the root store is a perfect use case for @EnvironmentObject - except it can't be used in this way because @EnvironmentObject only works with ObservableObject and Store is not, only ViewStore objects are observable.

That's not to say you couldn't work around this by, for instance, wrapping the store in a wrapper class that implements the ObservableObject protocol (even if it never triggers a change notification). Has anyone tried this? Is this a bad idea for reasons I'm missing?

1 Like