question
I'm trying to use event.preventDefault()
the method but keep getting an error. It says event
it has been deprecated.
<div id="item" onClick={
()=>test(event)} > </div>
“event”已弃用。ts(6385)
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 Window
property 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 window
the event instead of onClick
the event passed to the callback.
Just do this
<div id="item" onClick={
(event)=>test(event)} > </div>
The warning message will disappear.