Skip to content

How to modify dependencies of child views in SwiftUI previews? #74

Answered by tgrapperon
chwo asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @chwo! @Dependency is not meant to be used in View, because we don't control the view's creation process, and we're not able to correctly propagate dependencies across the view tree.
One thing you can do however is to create an ObservedObject that hosts @Dependency properties, and share this model either directly, or using EnvironmentObject. Please note that if you store this model instance using StateObject, you may need a little more work to make sure that the dependencies are correctly captured. I'm explaining the problem and providing a workaround here.
It is otherwise fine to define dependencies overrides in previews, provided they're captured by the model and not by the view. I …

Replies: 2 comments 2 replies

Comment options

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

Answer selected by chwo
Comment options

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants