let id = this.props.currentRecipe; // the Id is just to select the object that I want to render
let recipes = this.props.recipes[id];
{recipes.products.map((product, id) => {
return (
<FormControl
key={product + '_' + id}
type="text"
value={product}
onChange={this.editProduct.bind(this, id)}
placeholder={"Product " + (id + 1)}
/>
);
})}
This is my child function where I call my parent function:
It’s difficult to help you without a full view of your app. For instance, why are you selecting the object you want to render with an index? Why are you calling this.props.handleEditProduct in another function? You may have good reasons for your choices, but with what you’ve given here, it’s a bit confusing. Could you post your whole project, either as a Codepen or just your repository?
Here we go. I am calling that function from my child component but I’m not sure if this is the right way. + my code is really messy but I am new to React and I will refactor it later.
On line 78, you’re binding an index to one of your functions. I would also do this for your edit function. Instead of passing your EditRecipe component all of the recipes, just get the one recipe it needs and pass that. As you edit the recipe, the component’s state will change. The button you use to save should call your edit function, just passing in the new recipe from the component’s state, eg
function handleEdit() {
this.props.onEditRecipe(this.state); // this function had the index bound in the RecipeBoxContainer component.
}
The problem with this plan is that you’d need to make a non-trivial change - instead of passing your recipes to the Recipe component and having it render each element, you’d probably need to move that iteration into the RecipeBoxContainer component, eg