Navigation is rendered twice #1366
-
I'm trying to create a small example app the has 3 views. Each view has a list of navigation links the goes to the next view when a row is selected.
The transition between the first and second view works great but from the second to third transition loads the third view twice. Not sure if it's a SwiftUI bug or not. I'm thinking it's something in my state because I've done this with just vanilla SwiftUI and it works fine. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
@dadixon Whenever you encounter a WithViewStore(self.store) { … } Everything in the braces will be re-evaluated whenever any piece of state in In your case above, your -WithViewStore(self.store) { viewStore in
+WithViewStore(self.store, observe: \.selectedChild?.id) { viewStore in
List {
ForEach(viewStore.childStates) { childState in
NavigationLink(
destination: IfLetStore(
self.store.scope(
state: \.selectedChild,
action: SecondAction.selectedChild
)
) {
SecondView(store: $0)
},
tag: secondState.emotion.id,
selection: viewStore.binding(
- get: \.selectedChild?.id,
send: {
FirstAction.setNavigation(childID: $0)
}
)) {
Text(secondState.emotion.title)
}
}
}
.navigationTitle("First View")
} See the documentation on |
Beta Was this translation helpful? Give feedback.
-
This makes a lot of sense. Thank you. I didn’t know the WithViewStore had that feature. |
Beta Was this translation helpful? Give feedback.
@dadixon Whenever you encounter a
WithViewStore
like this:Everything in the braces will be re-evaluated whenever any piece of state in
self.store
changes, even if it only needs to observe much less state. In the case of complex flows like navigation, SwiftUI has bugs that can cause problems when a parent view is recomputed while you're drilled down to some other view.In your case above, your
WithViewStore
only needs to observe the binding'sid
: