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;