-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question on how to manage dependency between reducers #1518
Comments
I would handle this in an action creator using a) fire two actions, one for removing one for setting the new selected id In both cases you can randomly select a new item from your state in the action creator. |
Thanks @johanneslumpe. I haven't been using I can pass the existing list of todos to my add/remove todo action creators - it makes sense to me that a reducer would need to know where this data will ultimately be removed from or added to in order to calculate its result. It does mean every add/remove todo view requires the current list of todos, but at least it's explicit where the data came from. |
It’s best if you avoid passing large pieces of state as part of the actions. This creates a sort of “feedback loop” where it’s hard to trace where the data is coming from in case there is a mistake. |
I think that |
Thank you @gaearon, that makes perfect sense. |
I have a problem with a diferent kind of dependency: I'm creating a hamburgers delivery shop. How can I solve this problem? |
@ELIYAHUT123 : That's a usage question, and should really be asked on Stack Overflow instead of this issues tracker. That said, it sounds like you may be a bit confused. The reducer function call is 100% synchronous, so saying "the ingredients reducer is done" doesn't make sense. I think what you mean is "after the ingredients have been loaded from the server". But yeah, ask that on SO - you'll get more attention and a better response than here. |
@markerikson: Maybe I wasn't clear enought. |
Here is a link to the question on SO: |
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.
The text was updated successfully, but these errors were encountered: