IfLetStore .onAppear or .task gotcha #804
-
I've encountered a setup where In my setup, I have a list of optional states, each corresponding to the value of some enum (the states need to stay alive once initialized, even if the case changes, so I'm not using At the beginning, all the states are Group {
switch viewStore.kind {
case .first: IfLetStore(store.scope…
case .second: IfLetStore(store.scope…
}
}
.onAppear { viewStore.send(.initialize) } The In other words, I don't know if it's worth adding a note in the documentation of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hey @tgrapperon, yeah this is definitely tricky, but it is indeed how SwiftUI and TCA is expected to work, and this exact thing would happen in vanilla SwiftUI. It's tripped me up a number of times, but you have to think of view modifiers applied to Group {
IfLetStore(...)
IfLetStore(...)
}
.onAppear { ... } Is really: IfLetStore(...)
.onAppear { ... }
IfLetStore(...)
.onAppear { ... } Further, if condition {
content
} so your original code further transforms into something like this: if condition {
content
.onAppear { ... }
}
if condition {
content
.onAppear { ... }
} And so from this it becomes more clear that If you really need to kick off the initialization work without there being a view visible on the screen you might have to resort to some hacky things, such as |
Beta Was this translation helpful? Give feedback.
-
Also I'm going to convert this to a discussion since it's not really a bug with the library. |
Beta Was this translation helpful? Give feedback.
Hey @tgrapperon, yeah this is definitely tricky, but it is indeed how SwiftUI and TCA is expected to work, and this exact thing would happen in vanilla SwiftUI.
It's tripped me up a number of times, but you have to think of view modifiers applied to
Group
as being applied to every view inside. So:Is really:
Further,
IfLetStore
is really just:so your original code further transforms into something like this:
And so from this it bec…