0

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!

1
  • 1
    Value of onClick prop should be a function that can be called as a response to a click event but you haven't assigned a function to onClick prop because you are calling the handleClick function yourself. What you should do is assign an anonymous function to onClick prop and from inside of that anonymous function, call handleClick function: onClick={() => handleClick('/path/to/myUrl')} Commented Oct 8, 2020 at 12:33

4 Answers 4

2

This is because you are calling the function in this part <Col className="aslink" onClick={handleClick('/path/to/myUrl')}> instead of providing reference to it to be used on users click action. What you can do is define it like this:

const handleClick = (myLink) => () => {
  window.location.href=myLink;
}

then it will work as you want it.

Sign up to request clarification or add additional context in comments.

Comments

1
handclick('your path')

is already running the code. Try

onClick = {() => handlick('your path')}

This will stop it from automatically running

Comments

0

First off, I would recommend using React Router's Link over history.location.href, as it uses the routers history api, using this declarative, accessible navigation instead of history means that you are safe to any changes to the history api in the future.

You can import it like this:

import { Link } from 'react-router-dom'

Secondly, you were calling the handleClick function instead of executing the function.

Comments

0

If you use react-router*(which is most possible - if not - then you should research value of this)* then you can get access to browser-history via react router provider

  1. pass router api to your component
  • if you use modern react version - use hook useHistory -
const Comp = () => {
  const history = useHistory()

  const handleRedirect = useCallback((path) => {
    return () => {
      history.push(path);
    }
  }, [])

  return <div onClick={handleRedirect('path-to-page')}>Navigate</div>
}

export default Comp;


or 2. extract history object from taken props in your component

  • you can wrap you component by HOC - withRouter.
const Comp = ({history}) => {
  const handleRedirect = useCallback((path) => {
    return () => {
      history.push(path);
    }
  }, [])


  return <div onClick={handleRedirect('path-to-page')}>Navigate</div>
}

export default withRouter(Comp)

Comments

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.