I am making a recipe box project using React, and to edit the existing recipe, I tried this code, this.state.recipe
is currently an array of recipe objects:
editRecipe = (recipe) => {
this.state.recipe.find(obj => obj.name === recipe.name).name = recipe.name;
this.state.recipe.find(obj => obj.name === recipe.name).ingredients = recipe.ingredients;
}
And pass this method to the <EditRecipe edit={this.editRecipe} />
as a prop. However, when I activate the method in that component than invoke the edit prop, there is an error message that says : "this.state.recipe.find(...) is undefined"
Is there any other approach to edit the recipe?
Here is the error message that I got
Thank you.
It should have the this
avalable for the EditRecipe component, so change the way you pass the function as props
<EditRecipe edit={this.editRecipe.bind(this)} />
Also you should change the function as,
editRecipe = function(recipe){
let selectedObj;
selectedObj = this.state.recipe.find(obj => obj.name === recipe.name)
if(selectedObj){
selectedObj.name = recipe.name;
selectedObj.ingredients =recipe.ingredients;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments