0

I had properly working link with JS onclick attribute on a CMS, the code looked like this:

<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. 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. 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.

How should it be done correctly with this notation?

1
  • React doesn't onclick attributes to the html. It rather does event delegation. Also, your onClick prop is invalid. It should be more like this onClick={() => klaro.show()}, but I think you are trying to do something else. Commented Oct 2, 2022 at 16:23

3 Answers 3

1

Pass a reference to of klaro.show to the onClick prop. This will tell React to call the klaro.show method when you click.

<a className="button is-success" onClick={klaro.show}>člick</a>
Sign up to request clarification or add additional context in comments.

4 Comments

doesonClick={klaro.show} behave the same as onClick={() => klaro.show()} return-wise?
No, onClick={() => klaro.show()} will return the result from klaro.show(), onClick={klaro.show} will not. But you don't need to use return as you're not doing anything with the result that klaro.show() produces.
they might return false to prevent default action of the <a> tag;
The anchor tag doesn't have an href attribute. So it wouldn't do anything otherwise by default.
1

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>

Comments

0

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>

3 Comments

You're mixing Vanilla JS and React.
Thank you, the <a class="button is-success" onClick={() => klaro.show()}>{t('click')}</a> works as expected
Yes for react you have to use lots of keyword differently. See React Official Documentation for example of some attributes class -> className for -> htmlFor onclick -> onClick onfocus -> onFocus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.