DEV Community

Cover image for Stop Prop Drilling Like It's 2019 - SwiftUI @Environment Deep Dive
Karan Pal
Karan Pal

Posted on • Originally published at youtu.be

Stop Prop Drilling Like It's 2019 - SwiftUI @Environment Deep Dive

Stop Prop Drilling Like It's 2019 - SwiftUI @Environment Deep Dive

Okay, confession time. How many of you have this exact pattern somewhere in your codebase right now:

struct ContentView: View {
    @State private var userTheme: UserTheme = .system

    var body: some View {
        TabContentView(userTheme: userTheme) // 🤮
    }
}

struct TabContentView: View {
    let userTheme: UserTheme // I don't even use this!

    var body: some View {
        UserProfileView(userTheme: userTheme) // Just passing it along...
    }
}
Enter fullscreen mode Exit fullscreen mode

Yeah, we've all been there. Threading data through views that are just innocent bystanders in your data flow.

There's a better way

SwiftUI's @Environment property wrapper is one of those features that's been around forever, but most tutorials only show you the basics. You know, the classic "here's how to read the color scheme" example that doesn't help with real apps.

I just dropped a video that goes way deeper:

  • Built-in environment values that Apple gives you (but nobody talks about)
  • Creating custom environment keys that work with modern @Observable classes
  • Performance patterns that actually matter when your app has more than 3 views
  • Building a theme manager that updates instantly across your entire app

The real game changer?

No more prop drilling. No more views that exist just to pass data along. Your architecture becomes cleaner, your code becomes more maintainable, and you stop wanting to throw your laptop out the window.

Your turn

What's the weirdest prop drilling situation you've encountered? Drop it in the comments - I bet we've all got some horror stories to share.

And if this helped you clean up even one nasty view hierarchy, smash that heart button. It actually helps more devs find this stuff.


P.S. - If you're still using @ObservableObject for everything, this video will show you why @Observable is the way forward in iOS 17+. Trust me, your future self will thank you.

Top comments (0)