Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
How to use a element with href property in combination with react-router?

I want to use the navigation.push(url) functionality when clicking on an anchor tag element so that the app doesn't refresh by navigating to another page, and I can keep the application state.

The reason why I want to use this on an anchor tag element instead of a button is:

  • to keep the native functionality to right click the element and copy the url
  • to keep the url becoming visible when hovering over the element

When trying to use the combination as seen in the code below it still navigates towards a new page causing the webapp to refresh:

import React from 'react';
import { useHistory } from 'react-router-dom';

const TestComp = () => {
  const navigation = useHistory();
  return (
    <a
      onClick={() => {
        navigation.push(`/test`);
      }}
      href={`/test`}>
      this is a link
    </a>
  );
};

export default TestComp;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use the Link component from react-router-dom. It renders an anchor tag to the DOM and does all the linking/navigation for you that you are trying to do manually, and it doesn't reload the page.

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

const TestComp = () => {
  return (
    <Link to="/test">
      this is a link
    </Link>
  );
};

export default TestComp;
about 4 years ago · Juan Pablo Isaza Report

0

By using event.preventDefault() it prevents the browser to execute the norma function of a event (click)... On anchor tag, it prevents the browser from navigating to another page, so that you can do it manually

<a
      onClick={(e) => {
        e.preventDefault()
        navigation.push(`/test`);
      }}
      href={`/test`}>
      this is a link
    </a>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!