I'm trying to simulate a link in react js clicking on a div. This is my code:
function handleClick(myLink){
window.location.href=myLink;
}
and here where I call it:
<Col className="aslink" onClick={handleClick('/path/to/myUrl')}>
<div>...</div>
</Col>
But it goes directly to the URL without clicking, so it starts an infinite loop. How can I solve it?
Many thanks in advance!
onClickprop should be a function that can be called as a response to a click event but you haven't assigned a function toonClickprop because you are calling thehandleClickfunction yourself. What you should do is assign an anonymous function toonClickprop and from inside of that anonymous function, callhandleClickfunction:onClick={() => handleClick('/path/to/myUrl')}