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

Welcome to our community

Be apart of something great, join today!

RidgeOa

Newbie
Messages
7
Reaction score
1
Points
3
Hello,

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

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

however, I am getting an error when I apply it in 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() so as 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 being 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

Top