I am new to React/Next development. I have the code below:
import React from "react";
const Car = () => {
var carCounter = 0;
function addCar(event) {
event.preventDefault();
carCounter++;
console.log(carCounter);
}
return (
<form>
<span className="car">
<button class="big-button" onClick={addCar()}>
Add Car
</button>
</span>
</form>
);
};
export default Car;
Obviously, this code is simplified. The root of my error was that the form was submitting when I pressed the button. However, the purpose of the button was to not "submit" the form. Rather, it was to increment a counter.
This is the error that was thrown above:
TypeError: Cannot read property 'preventDefault' of undefined
I went based of this thread:
Let me know what I can do!