-
My @Reducer
struct ContactsFeature {
@ObservableState
struct State: Equatable {
@Presents var addContact: AddContactFeature.State?
var contacts: IdentifiedArrayOf<Contact> = []
}
enum Action {
case addContactButtonTapped
case addContact(PresentationAction<AddContactFeature.Action>)
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .addContactButtonTapped:
return .none
case .addContact(.presented(.cancelButtonTapped)):
debugPrint("Parent Reducer addContact cancelButtonTapped")
/* ... */
return .none
case .addContact(.presented(.saveButtonTapped)):
debugPrint("Parent Reducer addContact saveButtonTapped")
/* ... */
return .none
case .addContact:
debugPrint("Parent Reducer addContact")
return .none
}
}
.ifLet(\.$addContact, action: \.addContact) {
AddContactFeature()
}
struct ContactsView: View {
@Bindable var store: StoreOf<ContactsFeature>
var body: some View {
NavigationStack {
/* ... */
}
.sheet(
item: $store.scope(state: \.addContact, action: \.addContact)
) { addContactStore in
NavigationStack {
AddContactView(store: addContactStore)
}
}
}
} My @Reducer
struct AddContactFeature {
@ObservableState
struct State: Equatable {
var contact: Contact
}
enum Action {
case cancelButtonTapped
case saveButtonTapped
case setName(String)
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .cancelButtonTapped:
debugPrint("Child Reducer cancelButtonTapped")
return .none
case .saveButtonTapped:
debugPrint("Child Reducer saveButtonTapped")
return .none
case let .setName(name):
debugPrint("Child Reducer setName")
state.contact.name = name
return .none
}
}
}
}
struct AddContactView: View {
@Bindable var store: StoreOf<AddContactFeature>
var body: some View {
Form {
TextField("Name", text: $store.contact.name.sending(\.setName))
Button("Save") { store.send(.saveButtonTapped) }
}
.toolbar {
ToolbarItem {
Button("Cancel") {
store.send(.cancelButtonTapped)
}
}
}
}
} Everything works as expected, except that the
I would've expected to only see 2 lines, the child reducer running once and then the parent reducer. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It’s a bug in SwiftUI the text field is writing twice to the binding. |
Beta Was this translation helpful? Give feedback.
It’s a bug in SwiftUI the text field is writing twice to the binding.