简体   繁体   English

React.js onClick 返回事件

[英]React.js onClick return event

I had properly working link with JS onclick attribute on a CMS, the code looked like this:我在 CMS 上与 JS onclick属性的链接正常工作,代码如下所示:

<a class="button is-success" onclick="return klaro.show();">click</a>

it was opening the model window. Now I'm trying to replicate this on an old React.js project.它正在打开 model window。现在我正试图在一个旧的React.js项目上复制它。 I remember, that there are a few language notations, but as I'm not working at all with this language it's difficult for me.我记得,有一些语言符号,但由于我根本不使用这种语言,所以对我来说很难。

I've these kind of links:我有这些链接:

    <li><a href="#link">{t('Link')}</a></li>
    <li><a class="button is-success" onclick="return klaro.show();">{t('cookie choice')}</a></li>

But since it's React the last link is not working.但由于它是React ,所以最后一个链接不起作用。 I tried to change it to <a className="button is-success" onClick={() => "return klaro.show();"}>člick</a> but the onClick event is absent once the page is rendered.我试图将其更改为<a className="button is-success" onClick={() => "return klaro.show();"}>člick</a>但是页面呈现后onClick事件不存在.

How should it be done correctly with this notation?应该如何使用此表示法正确完成?

Pass a reference to of klaro.show to the onClick prop.onClick klaro.show This will tell React to call the klaro.show method when you click.这将告诉 React 在您单击时调用klaro.show方法。

<a className="button is-success" onClick={klaro.show}>člick</a>

People already answered the question correctly;人们已经正确回答了这个问题; but there is another dirty unclean way to do that as well;但是还有另一种肮脏不洁的方法可以做到这一点; I don't recommend it , just for your information:我不推荐它,仅供您参考:

<li dangerouslySetInnerHTML={{__html: `
   <a class="button is-success" onclick="return klaro.show();">click</a>`
}}></li>

Use this用这个

<li><a href="#link">{t('Link')}</a></li>
<li><a class="button is-success" onClick={klaro.show}>{t('cookie choice')}</a></li>

Or Use Like This或者像这样使用

<li><a href="#link">{t('Link')}</a></li>
<li><a class="button is-success" onClick={() => klaro.show()}>{t('cookie choice')}</a></li>

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

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