TCA - send an event from one reducer to another/modify the state of a different store #2067
Replies: 1 comment
-
Hey @beaneymidnite.
Nothing! If you embrace composition. In your case, the
var body: some ReducerProtocolOf<TodoListFeature> {
Reduce<State, Action> { state, action in
// the content of your `reduce` function
}
.forEach(\.todoItems, action: /Action.todoItems) {
TodoItemFeature()
}
} Now, each action received by a Please note that you'll also need a way to update your To use it in a
The |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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
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:and then
TodoItemFeature
something like this: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 in. How do I get the combined reducers to talk to one another? I considered adding some kind of delegation pattern, where
TodoListFeature
becomes a delegate ofTodoItemFeature
, but this feels like it's working against the pattern and that there is probably something in the library that I'm missing.I have a pal who is versed in Redux and he says I should just have one reducer called
TodosReducer
that combines both of these reducers. That may be the case here - but I don't believe for a second that a more complex app won't have side effects in ReducerA that ought to update state in ReducerB. How do we deal with that?Beta Was this translation helpful? Give feedback.
All reactions