React Router and Anchor do not work together
See original GitHub issueI am setting up a new React project using Grommet. Grommet Anchor and React Router Link won’t work together. Here is my requirements:
- The links should be stylized by Grommet. I am using
Anchor
component for this and UI theming is controlled by Grommet. - I am using React Router, and the app heavily depends on
Link
component.
Reasoning behind why this is crucial
- I want the user to switch between different themes. Since the theming is controlled by Grommet, I want to use components that are naturally reactive to the theme and theme context.
- I cannot use bare bone
<Anchor href=
because it causes page reload. On the other hand,<Link
jumps to the URL without page reload / flicker. - A suggested method elsewhere is to use
<Anchor onClick={()=> props.history.push('/path/123')} ...
. This is not desirable because I lose the semantic and the behavior of an anchor. User would not be able to perform “copy the link URL” or “open the link in new tab” features (by right clicking on desktop or long pressing on phone). It is not the right way. You’d use button instead. - Another suggested way was to wrap the component in the following manner:
It does not work. The Grommet theme is not rendering the Link here.<Anchor as="span"> <Link to="/path/123"/>Path 123</Link> </Anchor>
- Yet another post suggests to user
<Anchor as={Link} ...
does not work.
Expected Behavior
Grommet should have a way to wrap Link
and NavLink
of React Router – which is probably the most commonly used routing library.
Actual Behavior
Anchor
does not work desirably with Link
.
References
- https://github.com/grommet/grommet/issues/2855#issuecomment-485305773
- https://github.com/grommet/grommet/issues/2463#issuecomment-441462312
- Getting rid of browser warning when using Grommet Anchor as Link from react-router-dom
- Relevant discussion on Slack
Your Environment
- Packages used:
"grommet": "^2.7.11", "react-router-dom": "5.0.1", "styled-components": "^4.4.0"
- Chrome latest
- MacOS Majave
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Using anchor tags in react-router 4 - Stack Overflow
I can see the hash anchor tag in the url and in my redux store when I click on the anchor link in...
Read more >5 Remarkable React Router Features (Anchor Links, Query ...
The problem is that React Router will direct you to the main domain website.com/catsvsdogs and not append the post slug to the /pets/ ......
Read more >Create A Hash Anchor Link Effect With React-Router
To do this, I used react-router's Link component. I had used the NavLink for my navigation bar to take advantage of its styling...
Read more >Link - React Router: Declarative Routing for React.js
A function to which current location is passed as an argument and which should return location representation as a string or as an...
Read more >Creating a Link Between Pages in React Router
Well, we're not, because React Router has a better solution: the <Link> component. This is a wrapper around our old friend the HTML...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m using typescript and customize the
Anchor
this way:Usage:
And it works!
– UPDATE:
Omit
colorProp, hasIcon, hasLabel, focus
props before passing it toLink
component to avoid browser warnings.My current solution for a routed button is:
I add the
href
so that the browser detects that it’s a “routed button.” I cancel the event, then push to the history stack.Example: