Ad

Can I Safely Use Both The "to" And "onClick" Props On A React Router Link?

- 1 answer

I'd love to keep the benefits of using the default routing mechanism via the to prop, but also add additional logging logic for when the link gets clicked.

Ad

Answer

What logic do you want to achieve? And what's stopping you from adding onClick handler to Link component?

<Link to="/" onClick={event => { alert('home') }}>Home</Link>

Another possible option would be to Link to another component where you could put whatever logic inside the lifecycle method like componentDidMount().

Go to sandbox

Answering your question, there is a support for onClick event in the source code. So I guess it's totally fine.

Ad
source: stackoverflow.com
Ad