Solution when prompting "event" is deprecated

question

I'm trying to use event.preventDefault()the method but keep getting an error. It says eventit has been deprecated.

 <div id="item" onClick={
    
    ()=>test(event)} > </div>
“event”已弃用。ts(6385)

Insert image description here

reason:

A separate question is why you get a "deprecated" warning, the reason is:

https://developer.mozilla.org/en-US/docs/Web/API/Window/event

Read-only Windowproperty Events returns the event currently being handled by site code, the value is always undefined outside the context of the event handler.

You should avoid using this property in new code and instead use the Event passed to the event handler function. This property is not universally supported, and even if it was, it would introduce potential vulnerabilities in your code.

In other words, the "event" really should be passed as a parameter to the JS event handler . You shouldn't use global objects; you shouldn't use global objects.

Here are some good tutorials:

Solution:

The problem is that you are trying to use windowthe event instead of onClickthe event passed to the callback.

Just do this

 <div id="item" onClick={
    
    (event)=>test(event)} > </div>

The warning message will disappear.

Guess you like

Origin blog.csdn.net/qq_44721831/article/details/127070305