Web Hosting Forum - Net Hosting Talk

We are a community of individuals and businesses passionate about web hosting. Let's build, learn, and grow together.

React Why does using e.preventDefault() in this way in React not work?

RidgeOa

Novice
Member
Hello,

I see in the docs that e.preventDefault() is supported:

https://react.dev/learn/responding-to-events#preventing-default-behavior

However, I get an error when I apply it the following way. In the returning JSX, I'm adding an onClick() event handler to one of the HTML elements and passing into that handler as an argument an already-invoked deleteBtn() callback function (pic 1), the callback of which activates an alert box and console. Logs something.

I have this deleteBtn() handler set to receive an e (event), and the first line of the function is e.preventDefault() to prevent the rest of the function from continuing to be executed. Or at least that is what I assumed it would do. (pic 2)

When I set it up this way and load the page, the deleteBtn() is invoked, and then it gets to my e.preventDefault() line and throws a "Cannot read properties of undefined (reading 'preventDefault')" error (pic 3)

Why is e.preventDefault() not being recognized here?
 

Advertisement

Back
Top