Hey guys, I have been working on this problem for a solid week now and I am getting nowhere.
What happens is that I have an onClick event that triggers a function that is setup inside the parent element and for whatever reason when I call on the function(click the button) it tells me this ‘_this2.deleteRecipe’ is undefined.
Here is the code I am working with. Thanks so much for helping me. Many of the functions are not set up in any way, but once I get deleteRecipe working the other functions will follow its pattern.
I changed that bit of code and it gives me the same error.
Also, if I change this.state.deleteRecipe(item.id) to this.deleteRecipe(item.id) or any other path it gives this error. TypeError: undefined is not an object (evaluating ‘_this2.deleteRecipe’) or TypeError: undefined is not an object (evaluating ‘_this2.state’) depending on what directly follows ‘this’. Basically it has to do with where I am referencing ‘this’ from but I am inside the same component. I don’t know why it won’t work like everywhere else in the component. If I just tell it to console.log(this) it will return undefined. Something must be wrong with how I am using ‘this’.
I am really stumped, any help is greatly appreciated.
It looks like you’re confusing some of the things about React and ES6 classes.
I’ve moved some stuff around to help move you into the right direction. One thing I want to point out is that the functions for the class are not part of this.state.
this.state holds data, like the recipeList. Functions that operate on the state go outside of the constructor, like I’ve shown below. Then, to modify the state, you’d use this.setState({ /* new state */ });
class Recipe extends React.Component {
constructor(props) {
super(props);
this.state = {
recipes: recipeList,
};
}
editRecipe(key) {
// do things
}
addRecipe() {
// do things
}
deleteRecipe(id) {
// do things
}
//this is were the onClick issues come from.
render() {
var dispList = [];
this.state.recipes.forEach(function (item, i) {
console.log();
dispList.push(<div key={ "key_" + i }><h3>Recipe: { item.recipeName } - <button onClick={ () => { this.state.editRecipe(item.id) } }>Edit</button> <button onClick={ () => { this.state.deleteRecipe(item.id) } }>Delete</button></h3><p>Ingredients: { item.ingredients }</p><br /></div>);
})
return (
<div>
{ dispList }
<button onClick={ () => { this.addRecipe() } }>Add Recipe</button>
</div>
)
}
}
Thanks mblood! I reorganized the code like you did and now my functions are outside of ‘state’. Sadly, That isn’t fixing the problem. It still gives me the 'TypeError: undefined is not an object (evaluating ‘_this2.deleteRecipe’)
Why does ‘this’ read out with a underscore and a two next to it? ‘_this2.’
Thanks for the help. I really hope someone can figure this out.
Here is the code as I have it now. Should I update the code at the top of this post or will that just confuse people? Am I incorrectly binding them in this? Thanks.
I just tried that and it makes my functions try to execute on page load. That is why I put the arrows in originally, but even more depressing is that when I refreshed the page wouldn’t load because it conked out on the same old ‘_this2.’ error. Thanks so much for the help. I hope we can figure this out.
Wow, that is really helpful. I am understanding what you are showing me. Thanks. I still have some questions though, maybe you would have some insight.
I am currently thinking that the reason why my pen doesn’t work has something to do with my method of using a for loop to create the list in lines of JSX contained in dispList. Is it possible that when I reference ‘this’ that is doesn’t know what ‘this’ is because of the way it is pushed into a array(dispList) and then rendered from that? Whenever I console.log ‘this’ from inside the for loop, it comes back undefined.
Any thoughts? If that is it, do you have any other ideas for creating a list without pushing into an array?
Your dispList is empty array and it doesn’t get updated. Render is only run on state change (and you can’t change state in render function).
You should make dispList a function and use map to iterate over array.
I realize this has been dead for a while, but the issue was the following: @escottalexander was using the following code to loop over his items,
The troubling line in the code is the function inside the forEach loop. This function must also be bound to the context of the component, using one of the two methods that @jenovs mentioned:
So if in the forEach loop one used a function such as this, it should work correctly.
this.state.recipeList.forEach((function (item, i) {
/* render a single recipe */
}).bind(this));
// OR
this.state.recipeList.forEach((item, i) => { // this works because a 'fat arrow function' is automatically bound to the context in which it is first defined
/* render a single recipe */
});
The functions were being bound correctly inside the forEach loop, but the function that was being called to do this is what it was being bound to instead of the component. I came accross this thread while trying to fix my own issue where the error was mentioning an unknown “_this2”, and now I have a clue that I’m probably forgetting to bind one of my functions along the way. I hope this helps anyone stumbling across this in the future! (Hello future!)