Hey @jecht83 ! I think it isn't a good practice to call actions from parent to child. Usually, I tend - to get a more coherent system - to re-create child states. Please, look at this example, which handles an identified array of states, to check if it fits your case.
It is not recommend to share logic between domains by sending actions like this. We have this details in the docs.
The article above describes some alternatives, such as a mutating func on the child state, and the way you use that technique with StackState is like the following:
case let .element(id: id, action: …):
state.path[id: id]?.someMutatingFunc()
By following this approach added a mutating func into the Child and got the error
// Path.State does not contain mutating func
case let .element(id: id, action: …):
state.path[id: id]?.someMutatingFunc()
// Cast from 'ContentFeature.Path.State?' to unrelated type 'SettingsFeature.State' always fails
case let .element(id: id, action: …):
if let settingsState = state.path[id: id] as? SettingsFeature.State {
settingsState.someMutatingFunc()
}
return .none
Also documentation recommends another approach, is this working with navigation stack?
case .buttonTapped:
return Child().reduce(into: &state.child, action: .refresh)
.map(Action.child)
I'm guessing that Path.State is an enum of all the features that can be pushed on the stack, and so that is why there is no mutating function. You are probably wanting to call a mutating function on a particular case of the enum.
You can do this using the [id:case:] subscript on StackState:
OK one last thing, I can not make the reduce subscript to work with navigation stack store
I've tried the following with no success:
return SettingsFeature()
.reduce(into: &state.path[id: id, case: \.settings]!, action: .refresh)
.map(Path.Action.settings) // Type of expression is ambiguous without a type annotation
@mbrandonw, how can I make it work with StackStore?
Hi @mbrandonw do you know if i can access a different element id inside the stack while the current id is different? maybe hardcode the id number and access the state, for instance:
let customID: 3 as StackElementID
var customState = state.path[id: customID, case: \Settings]
if isn't possible, which looks like it isn't since it does crashes.
how could i update a different type of state while receiving an action from my root reducer from a different state, for instance:
case let .path(.element(id: id, action: .photoFiltering(.nextButtonTapped))):
// here access different path and change property inside it