Passing data down the view tree in SwiftUI
iOS SwiftUI EnvironmentValues dataflow Estimated reading time: 4 minutesEnvironmentValues
is a set of values that may be used during interface and app functionality building.
This is a simple and yet powerful addition from SwiftUI
, that can improve any state and dataFlow in the app.
A collection of environment values propagated through a view hierarchy. (Apple)
EnvironmentValues
By itself, EnvironmentValues
is a struct with a collection of values. We may create an instance of this struct and check it out:
simple po
command gives us not to much info:
po values
▿ []
▿ plist : []
- elements : nil
- tracker : nil
But, according to official doc - we may access thought @Environment
and keypath to a lot of them. To figure out what if under the hood we may use Mirror
:
▿ AnyCollection<(label: Optional
, value: Any)> ▿ _box : <_RandomAccessCollectionBox<LazyMapSequence<Range
, (label: Optional , value: Any)>>: 0x600002314820>
Aha, this is a struct that uses lazy collection within these values.
The usage of these values is very important in SwiftUI
. Values can be used implicitly by setting them for any of the view tree:
where debug
is extension to View that prints Mirror(reflecting: self).subjectType
.
thanks to objc.io for this
The type will be next:
We also know that we can apply modifiers to some stack, and then all nested children that can apply these settings should apply these values.
And if we check the type
You can see, that Type is the same, so this is just a wrapper for environment values that can be used on our own.
This means that we can use the environment to create custom modifiers and functions that can change the appearance of our view’s even on high-level view tree view’s.
there is an open-source WIP project that tries to implement SwiftUI within
_EnvironmentKeyWritingModifier
, check it out for more.
Custom environment value
As mention on official doc, we can create a custom Environment value.
The steps are very simple:
- declare a new environment key type and specify a value for the required defaultValue property
here we will use this value for our own
View
with a checkmark shape inside
- use the key to define a new environment value property
This then can be used inside View
using the @Environment
variable:
And usage:
with changed environment:
We may go futher and add View extension as done within font in saple above:
Than apply to view tree:
Result:
Pros and Cons
Cons:
- easy to forget to pass the
@Environment
variable and no error until we not use the actual screen where it uses. The good point here -default
value will be used - easy to mismatch view-tree and set a variable in the wrong place
- ease to forget to pass
@EnvironmentObject
- in comparison to@Environment
, the default value is missed here, and do crash will be received.
Pros:
- easy customization
- easy injection
Share on: