Combining optional reducers is tricky and can lead to subtle bugs when done out of order, which is why we have these preconditions, but we could probably improve the documentation better with some fleshed out examples. One problem with hiding an action in a .onDisappear
is that action is sent when state is nil
it will be ignored, and you might miss out on performing some logic that you depend on there.
So instead of using .onDisappear
you can either leverage the sheet's onDismiss
action closure, or better yet, avoid the constant binding and instead derive a binding from the view store:
.sheet(
isPresented: viewStore.binding(
get: { $0.detailState != nil },
send: { $0 ? .presentDetail : .detailAction(.dismiss) }
)
) { ...
For another example, @darrarski just ran into a similar issue here: IfLetStore and Effect cancellation on view disappear
We're replying to it now.