But for some reason when I use onClick={this.updateCart} in my services component it will crash and tell me that it is undefined.
The function works fine in my app.js (i did some testing) and the state does update in the other components as well. The function is the only issue.
This is my first time passing a function like this, so I am expecting that I made a stupid mistake. Regardless, thank you for your time.
I have this above my setState
this.updateCart = this.updateCart.bind(this);
I tried it with and without the ‘props’ just to see and both throw the same error.
So I narrowed down the problem. I made a testfunction() with just a simple console log and tried passing it to the services component. It had the same error.
document.addEventListener('click', e => {
if (e.target.classList.contains('button')) {
this.props.updateCart()
}
});
This was my solution, if anyone is curious.
The problem was the function was rendering before the HTML elements. So this is how I got around it. I am just sharing it just in case someone else has a similar issue in the future.
I still appreciate the help I get here at FCC!