简体   繁体   English

有没有办法禁用React的警告“无效的事件处理程序属性`onclick`”?

[英]Is there a way to disable the warnings “Invalid event handler property `onclick`” of React?

I'm building a webapp with React where I need to display some ads. 我正在用React构建一个Web应用,我需要在其中展示一些广告。 The problem is as follows: 问题如下:

I need to display these ads inside a React component, I've been give the html code of these ads and they work fine (So far) but they throw a big warning: 我需要在React组件中显示这些广告,已经给出了这些广告的html代码,它们可以正常工作(到目前为止),但它们发出了一个警告:

    index.js:1375 Warning: Invalid event handler property `onclick`. Did you mean `onClick`?
        in a (at ad-display.component.jsx:4)

I do know that the reason of the warning is the onclick property that should be onClick instead, however I don't want to mess up with the original HTML code that has been given to me, in case it would affect my client's revenue 我确实知道警告的原因是应该将onClick属性改为onClick,但是我不想弄乱已经提供给我的原始HTML代码,以防它会影响客户的收入

The code of the ad is a simple a tag: 广告代码是一个简单的代码:

    <a onclick="setSomething(event, this)" href="http://coollink.com" id="rt_aff_banner_514" border="0" target="_blank"><img border="0" src="https://coolimage/1543860030.png"/></a>

So I was wondering if there is a way to disable these warning, cause it's getting very dificult to debug with them. 所以我想知道是否有一种方法可以禁用这些警告,因为很难对它们进行调试。

Also, feel free to scold me if the way I'm doing this is all wrong and I would need to insert this html code in any other way into my React code 另外,如果我做这件事的方式是错误的,请随时责骂我,我需要以任何其他方式将此html代码插入我的React代码中

Thanks in advance! 提前致谢!

I think what you're looking for is dangerously set inner html: 我认为您要寻找的是危险地设置内部html:

const ads = `<a onclick="setSomething(event, this)" 
   href="http://coollink.com" id="rt_aff_banner_514"
   border="0" target="_blank">
    <img border="0" src="https://coolimage/1543860030.png"/>
   </a>`

<div id="ads" dangerouslySetInnerHTML={{ __html: ads }}></div>

@HanselDoullery It's a syntax warning for react. @HanselDoullery这是对react的语法警告。 You are binding html event handler onclick , it will work as normal event handler but react compiler will give you warning as in react syntax for binding click event is slightly different. 您正在绑定html事件处理程序onclick ,它将像普通事件处理程序一样工作,但是react编译器会像在警告语法中一样为您提供警告,用于绑定click事件的方式略有不同。 Please refer this link . 请参考此链接

In your case you will write your click handler something like this: 在您的情况下,您将为click处理程序编写如下内容:

<a onClick={()=> { setSomething(event, this)}} ...>
....
</a>

If you have a global function you could try this: 如果您具有全局功能,则可以尝试以下操作:

<a onClick={(e)=> { window.setSomething(event, this); }} ref="http://coollink.com" id="rt_aff_banner_514" border="0" target="_blank">
....
</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM