-
Hey guys, I have been using composable architecture with UIKit for few months, and I still cannot find a better way to figure it out that how to update the child state from the parent reducer things is this, think we have a struct Parent: ReducerProtocol {
struct State: Equatable {
......
var child: Child.State?
}
enum Action: Equatable {
case refresh
}
......
var Body: some ReducerProtocol<State, Action> { Reduce { state, action in
switch action {
case .refresh:
/* some parent state update */
// then update the child state by trigger the action called `Child.Action.refresh`
return .none
}
}
struct Child: ReducerProtocol {
@Dependency(\.database) var db
struct State: Equatable {
......
}
enum Action: Equatable {
case refresh
}
......
var Body: some ReducerProtocol<State, Action> { Reduce { state, action in
switch action {
case .refresh:
/* some child state update from db */
state.a = db.findA()
state.b = db.findB()
return .none
}
} things is, some times there may have no directly connection from parent state to child state, which means that I cannot using the compute property to calculate the new child state like we have modified some database rows out of the I have discovered several ways to solve this,
I think non of these solved my problem in some way, and I just want to know if there is an elegant and simple method to help me fix this, if so that would be helpful |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To make the parent send actions to the child, the parent feature should be composed with the child feature. For example you can add a case child(ChildFeature.Action) to the parent actions. Make the parent reduce func or body run the child with ifLet or Scope. Then In your parent refresh action return Effect(value: .child(.refresh)) instead of .none. There are multiples ways of doing this depending on the relation of you features. If they are unrelated you could have a third reducer that compose your features and relay the action |
Beta Was this translation helpful? Give feedback.
To make the parent send actions to the child, the parent feature should be composed with the child feature.
For example you can add a case child(ChildFeature.Action) to the parent actions. Make the parent reduce func or body run the child with ifLet or Scope. Then In your parent refresh action return Effect(value: .child(.refresh)) instead of .none.
There are multiples ways of doing this depending on the relation of you features. If they are unrelated you could have a third reducer that compose your features and relay the action