TCA - send an event from one reducer to another/modify the state of a different store #2068
-
I'm new to TCA 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 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 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 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 update the todo... 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 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 in. How do I get the combined reducers to talk to one another? I considered adding some kind of delegation pattern, where I have a pal who is versed in Redux and he says I should just have one reducer called |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
State sharing can be tricky in TCA! Do not fret, you'll soon get the hang of it :) The construct you're looking for is See: I also encourage you to look at the case studies. One example is a mini Todos app! It should contain the best practices, feel free to let me know any questions you may have. |
Beta Was this translation helpful? Give feedback.
State sharing can be tricky in TCA! Do not fret, you'll soon get the hang of it :)
The construct you're looking for is
forEach
, where you swap out yourtodos
array with anIdentifiedArray
of yourTodoListFeature.State
and put in theTodo
struct inside theTodoListFeature.State
.See:
swift-composable-architecture/Examples/Todos/Todos/Todos.swift
Line 102 in 8330f53
I also encourage you to look at the case studies. One example is a mini Todos app! It should contain the best practices, feel free to let me know any questions you may have.