A different approach here could be to inject the initial TState
value in the constructor and do-away with the @EnvironmentObject
completely. Then from the parent view you can use the @EnvironmentObject
value when creating the view.
struct TEditorView: View {
@State var name = ""
init(tState: TState) {
self._name = State(initialValue: tState.name)
}
var body: some View {
...
}
}
struct ContentView: View {
@EnvironmentObject private var tState: TState
var body: some View {
TEditorView(state: tState)
}
}
Or use a @Binding
instead of @State
if the name
value is meant to be two-way.
In general I'd also question why you need the @EnvironmentObject
in the constructor. The idea is with a @EnvironmentObject
is that it's represented the same in all views, so you should only need it body
.
If you need any data transformations it should be done in the object model itself, not the view.