Replies: 1 comment
-
Hi @andtie, the first issue, that of over-observing, is to be expected. If the case of an enum holds non- You can see this concretely if you default your struct EnumBugView: View {
let store: StoreOf<EnumBugFeature> = .init(initialState: .loggedIn(SomeOtherFeature.State())) {
EnumBugFeature()
}
var body: some View {
let _ = Self._printChanges()
switch store.state {
case .loggedIn:
Text("LoggedIn")
Button("Test") {
store.send(.noop)
}
case .loggedOut:
Button("Test") {
store.send(.noop)
}
}
}
} And it's because we are able to see that the However, that doesn't mean there isn't something to improve here. We technically could special case And then as for the And further, you are using If you were to replace the struct InfiniteBugEnumView: View {
let store = StoreOf<EnumBugFeature>(initialState: .loggedOut) {
EnumBugFeature()
}
@State var isCoverPresented = true
@State var isSheetPresented = true
@State var isOtherSheetPresented = true
var body: some View {
ZStack {
Text("count: \(store.state)")
}
.fullScreenCover(isPresented: $isCoverPresented) {
NavigationStack {
Form {
Text("count: \(store.state)")
.sheet(isPresented: $isSheetPresented) {
NavigationStack {
ZStack {
Form {
Text("count: \(store.state)")
}
.sheet(isPresented: $isOtherSheetPresented) {
Text("Leaf")
.onAppear {
print("onAppear", Date())
store.send(.noop)
}
}
}
}
}
}
}
}
}
} The Since this isn't an issue with the library I am going to convert it to a discussion. Please feel free to continue the conversation over there! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
When using the
@ObservableState
-macro in a reducer with enum based state, every action sent to the store triggers a re-render of SwiftUI-views.Checklist
main
branch of this package.Expected behavior
When sending an action to a store that does not update the state, a SwiftUI-view should not be re-rendered.
Actual behavior
The SwiftUI-view is re-rendered for every action.
Steps to reproduce
Consider the following feature and view:
Every press on the Button triggers a view update.
This is what I found debugging this issue
Part of the code generated by the @ObservableState macro is the following:
The problem seems to be that the id for the
loggedOut
-case is regenerated every time it is accessed. If I add an associated value to the case that is itself marked with @ObservableState the problem goes away and there are no unnecessary re-renders.Bonus
In the following code, this bug actually triggers an infinite rerender-loop in SwiftUI:
The Composable Architecture version information
1.11.1
Destination operating system
iOS 17.5
Xcode version information
Version 15.4 (15F31d)
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions