Id of element returning undefined, why?

Hello everyone, I am stuck on a very small problem.

 addToCart(e) {

I am trying simply to get the id of the element that is clicked in my other component.
The following is being maped from a JSON file

 <button id={serviceitems.title} className="button">Add to cart</button>

But for some reason no matter which way I do this it returns undefined.
I know this has to be a small error, but I just can’t figure it out. Please help!

I think we need to see more of the code. Does logging give the element you expect?

Thanks for your reply.
When I change it to, I get the same error.
my whole code is here :

You are not passing the event to the method in services.js this.props.addToCart(e);. You also didn’t bind it in App.js (in case you need this inside the method).

BTW, you have so many things missing in your dependencies. The code is supposed to be set up so anyone can clone the repo and just do npm install.

I would also strongly suggest installing Prettier and have it format your code.

1 Like

Thank you, I will try this when I am off of work.
This project has been my main project for the last 2 months or so, I have tried so many different methods and installed so many different dependencies that it’s full of bloat.
Is there a way to clean all the unused non packages?
And I will try prettier, thank you!

You can uninstall using npm the same way you install npm uninstall packageName. Or you can make sure you have all the dependencies you need in package.json, then delete the node_modules folder and do npm i to get just the packages from the dependencies.

But no matter what, if you have a dependency, like an import in some file, that dependency should be in the package.json from having been installed. So I’m not sure how your repo has the dependencies it does in package.json (or lack thereof in this case) but your code depends on packages not listed in the package.json file.

Did you not push the package.json when updating (add/remove) dependencies? It should be kept in sync with your code in the repo.

Thanks, this seems to have cleaned up my code considerably.
Also thanks for helping me out with my original problem!