1

I have a table and in each row there is one meal. Each meal consist of many ingredients. I want to be able to either delete ingredient and to delete meal. How should the state and the delete ingredient function look like?

Should the state look like this:

{
   meals: [{name:'breakfast', id:11}, ...]
   ingredients: [{name: 'eggs', id:111, meal_id:11}...] 
}

or this:

{
   meals: [{
       name:'breakfast',
       ingredients: [{name: 'eggs'}]
   }]
}

I think that the second option is better. And the most important question: How should the delete ingredient function looks like? Should each ingredient has its own unique id and should I dispatch a delete action passing ingredient id?

1 Answer 1

1

Meals should be the parent component. For each meal child component, the meal-id, name, ingredients should be passed as props. The meal should have a list of ingredient child components with ingredient details passed in. The id should be passed to both meal and ingredient component as each child component should have an unique key.

The event handlers for delete ingredient should be handled in the meal component where the corresponding ingredient is deleted and is reflected in the props. Similarly, the meal should be deleted by the parent Meals component and the new state should be passed down as props.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.