Any ideas why the reducers are receiving 2 times each action? #1093
-
So after entering "a" I'm receiving
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
That's some standard issue with struct ContentView: View {
@State var text: String = ""
var body: some View {
TextField(
"",
text: Binding(
get: { text },
set: {
print("Set:", $0)
self.text = text
}
)
)
}
} In this configuration the binding's setter is activated twice for each key stroke. This is caused by the way You can probably mitigate the effect by debouncing the action. Other solutions that would count the actions would make you assume that this behavior is constant, which is not guaranteed. Of course, if this isn't having any side-effect in your project, you can disregard the issue, as this is not caused by something you're doing wrong with TCA. As an aside, I would recommend to keep a handle to your root store somewhere: if for some reason the |
Beta Was this translation helpful? Give feedback.
-
I have an extension on func uniqueUpdates() -> Self {
var delta: (current: Value, next: Value)?
return Binding {
wrappedValue
} set: { newValue, transaction in
guard delta?.current != wrappedValue, delta?.next != newValue else { return }
delta = (current: wrappedValue, next: newValue)
self.transaction(transaction).wrappedValue = newValue
}
} |
Beta Was this translation helpful? Give feedback.
That's some standard issue with
Textfield
and you have the same issue in vanilla SwiftUI:In this configuration the binding's setter is activated twice for each key stroke. This is caused by the way
UI/NSTextField
are wrapped in SwiftUI, and I don't think there is much you can do prevent it.You can probably mitigate the effect by debouncing the action. Other solutions that would count the actions would make you assume that this behavior is c…