Subview is unnecessarily refreshed #1801
Replies: 3 comments 1 reply
-
The Composable Architecture version information: 0.48.0 and 0.46.0 |
Beta Was this translation helpful? Give feedback.
-
Hey @leiyutinghua! There are a few things you can do to work around the issue right now (if it's problematic in terms of performance). I presented one solution here, so maybe it could help in your case. The quick and dirty solution would be to put your store in This side-effect is usually not an issue, but I concur that, ideally, |
Beta Was this translation helpful? Give feedback.
-
Hi @leiyutinghua, it is a bummer, but in practice it may not actually lead to problems. We have plans to look into over-renders soon, especially once we can drop iOS 13 support, and we are hopefully that the new In the meantime you can automate @tgrapperon's suggestion with a little helper view: struct ScopeStore<ParentState, ParentAction, ChildState, ChildAction, Content: View>: View {
@State var scopedStore: Store<ChildState, ChildAction>
let content: (Store<ChildState, ChildAction>) -> Content
init(
_ store: Store<ParentState, ParentAction>,
state: @escaping (ParentState) -> ChildState,
action: @escaping (ChildAction) -> ParentAction,
@ViewBuilder content: @escaping (Store<ChildState, ChildAction>) -> Content
) {
self._scopedStore = State(wrappedValue: store.scope(state: state, action: action))
self.content = content
}
var body: some View {
self.content(self.scopedStore)
}
} …which can be used like this: // View1(store: self.store.scope(state: \.state1, action: APPReducer.Action.action1))
ScopeStore(self.store, action: \.state1, action: APPReducer.Action.action1) { store in
View1(store: store)
} Honestly it's not that different from the current style so it might be worth shipping this tool in TCA. I'm going to convert this issue to a discussion because I don't think there's anything to immediately solve, but it is something on our minds. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
A change in the State of the superview causes a refresh of the subview
Checklist
main
branch of this package.Expected behavior
Reducer code
View code
I hope that clicking on ContentView shouldn't cause View1 to refresh, but it does
Actual behavior
I hope that clicking on ContentView shouldn't cause View1 to refresh, but it does
Steps to reproduce
No response
The Composable Architecture version information
No response
Destination operating system
iOS 15
Xcode version information
Xcode 14.2
Swift Compiler version information
No response
Beta Was this translation helpful? Give feedback.
All reactions