0

I would like to make the href attribute of my anchor tag to call a method instead of going to some url. I know that in plain js I can do this:

..href={"javascript: foo(value)">

So I've done the same in my jsx file:

<a href={javascript: ()=>foo(value)}>Click Here!</a>

but it doesn't work. How it can be done?

Joe
  • 443
  • 1
  • 4
  • 21

2 Answers2

1

use reacts onClick

You can use onClick on an element in react. like this

<a onClick={()=>foo(value)}>Click Here!</a>

That being said, if the purpose of your a tag is not to go to a link then your html is incorrect and you should think about using a button tag instead

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81
-1

in React use onClick to do the job

<a onClick={() => doSomething("Hello") }>Click Me</a>