Feature Request: Allow injecting a custom component for NavLink to render to enhance support for HOCs
See original GitHub issueBackground: I’m using Radium to style my React components.
Radium works by wrapping the target component’s render function, searching for style props on DOM-node children and replacing them according to the current UI state (hovering/focusing/…). However, Radium does not do this for non-DOM-node children, since it does not know what the component is actually doing with the style prop. This is the case for React Router’s Link and NavLink components
Usually, this is solved by wrapping the React component using Radium’s HOC, as you can do with Link like in this snippet, which renders a Link with a red background, switching to green once you hover over it:
import React from "react";
import { render } from "react-dom";
import { BrowserRouter, Link} from "react-router-dom";
import Radium from "radium";
const RadiatingLink = Radium(Link);
const App = () =>
<BrowserRouter>
<RadiatingLink
to="/test3"
style={{ background: "red", ":hover": { background: "green" } }}
>
LinkTest
</RadiatingLink>
</BrowserRouter>
render(<App />, document.getElementById("root"));
But, if we were to replace Link with NavLink in this example it wouldn’t work, because NavLink internally renders a Link not wrapped by the Radium HOC.
To solve this problem, I propose to add a new “renderComponent” prop to NavLink, which defaults to Link and gets rendered instead of a hardcoded Link component. This allows users to wrap the Link component rendered by NavLink (Or even render something completely different).
If this were implemented, it would allow injecting a custom render component like this:
import React from "react";
import { render } from "react-dom";
import { BrowserRouter, Link, NavLink} from "react-router-dom";
import Radium from "radium";
const RadiatingLink = Radium(Link);
const App = () =>
<BrowserRouter>
<NavLink
to="/test3"
renderComponent={RadiatingLink}
style={{ background: "red", ":hover": { background: "green" } }}
>
LinkTest
</NavLink>
</BrowserRouter>
render(<App />, document.getElementById("root"));
This would also fix https://github.com/FormidableLabs/radium/issues/907
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)

Top Related StackOverflow Question
I also don’t like that NavLink can’t be wrapped in HOC.
For temporary solution I nested all NavLinks inside divs and styled background of those divs on hover with Radium. Maybe it helps somebody.
You can walk around this with a
Route. It’s even documented in the React Router Training.You can pass what to render as a
render prop(below) or you can write it directly within theBetterNavLinkas in the linked example. (above)Then in the render of your NavBar
result is