I'm new to TCA (The Composable Architecture) so I may have the entirely wrong approach here, so apologies if so I and I would appreciate getting pointed in the right direction. It's a little bit tricky to find resources for this since I've adopted the new ReducerProtocol approach to TCA and the vast majority of the tutorials/documentation I can find uses the old approach.
Essentially imagine I have two reducers, TodoListFeature and TodoItemFeature. The former displays a list of todo items, and the latter displays a single todo item, and allows editing of its information (e.g. title).
So TodoListFeature might look like this:
struct TodoListFeature: ReducerProtocol {
struct State {
var todoItems: [TodoItem]
}
enum Action {
case onAppear
case dataLoaded(Result<[TodoItem], Error>)
}
func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case .onAppear:
// perform async action to fetch todos, then dispatch .dataLoaded
case .dataLoaded(let result):
// update state
}
}
}
and then TodoItemFeature something like this:
struct TodoItemFeature: ReducerProtocol {
struct State {
var saveError: Error?
}
enum Action {
case updatedItem(TodoItem)
}
func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case .updateItem(let item):
// perform async action to fetch todos, then dispatch .dataLoaded
// But then what?
}
}
}
So this is the question - see the "But then what" comment? Essentially I now want to take that updated item, and update my state in TodoListFeature - in other words, I'd like to update the list.
I've read about composition, but this just seems to be arbitrarily grouping together features into a single store and then scoping that single store depending on the view we're using. How do I get reducers to talk to one another?
I'm trying to work this out too, and although in the early stages of investigation, haven't found an answer.
My only possible solution for your question though would be to ignore performing the async action to fetch todos, then dispatching
.dataLoadedand just dismiss that view. You already have the.onAppearaction set in the TodoListFeature` so that would be triggered when the TodoItemFeature view is dismissed.