Skip to content

Question on how to manage dependency between reducers #1518

Closed
@stuartkeith

Description

@stuartkeith

I'm writing my first redux application and have run into an issue.

Say I have a basic todo application - a reducer provides an array of todos. The view is a series of tabs with each todo's header on it. You can select a tab and that todo's edit view will appear below.

My first approach was to create another reducer that tracks the selected tab's ID (as "selected todo" in this case is UI state, I don't want to put it in the todo reducer's state). The reducer receives a "SELECT_TODO" action with the ID attached and updates accordingly. Then the view can render the tabs and the selected todo's edit view below. Pretty straightforward. A new todo could be selected automatically by the reducer listening for an ADD_TODO action and returning its ID.

What I'm stuck on is how to handle the user deleting the selected todo and having a new selected todo calculated automatically - I can have the "selected todo" reducer listen to a REMOVE_TODO action,
but in order to calculate a new value it needs access to the list of todos - it will try to select the next todo,
or the previous if the selected todo is at the end of the list.

Any suggestions on how to best structure things in this scenario? Since I'm new to redux it would be really helpful to get some advice - I have a few ideas that would technically work but I'm not sure if I'm on the right track or not.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions