Hi all! I want to apologize in advance as typically when I ask questions on here, or in any community, I'll include detailed screenshots and code examples, this time has to be an exception unfortunately. The app that I'm working on has too many moving pieces, it would take a small book to explain everything, but I have narrowed down the issue and just require a bit of help. Bear with me, I'll do my best to explain what is happening.
-
The functionality in mind is that a user will be able to pick how much of an ingredient they desire from a list of ingredients. The options are "None", "Light", "Normal", "Extra". The name of the ingredient is above a button with a "+" selector to increase how much, and a "-" selector. The way I accomplish this functionality is that I have a state that sits at the top of the tree. For each ingredient I receive, I map out an ingredient selector component that receives props.
-
The way I have it right now is that the ingredient selected is a piece of state passed through props. Here is the kicker -- it actually works, except for one little thing -- when you flip one of them, it flips all of them. The problem is likely that I'm passing the state down through props. When the state changes, it affects all of them. It seems as if there needs to be a way to check and only flip that buttons state
-
Here is the thing -- I've tried a few different methods to enact this change. I've tried comparing the ID of the clicked button (by using the event object) to the ID of the ingredient object. I've tried using a .map like in the squares example from the intro react course. I just feel like I am missing the mark here somewhere. I'm missing something.
-
The organization of my files look a little something like this
Index -(props)-> Modal -(props)-> Button -(ingredients.map to generate a button for each ingredient)
Index is where all of the state lives -
I could use derived state to solve this issue. Each ingredient could manage its own state. That seems to be worst practice and I'd like to stick away from derived state, although the call of the void has been getting louder and louder.
With all of those said, here is my overarching question. When you are mapping through and generating components based on your map, how can each one of them have their own version of state that isnt derived state? How can I only flip the state for the button that is clicked.
Thanks a bunch. I really appreciate the help from this community. Cheers!