Skip to content

Feature Request: Allow injecting a custom component for NavLink to render to enhance support for HOCs #5496

@Nimelrian

Description

@Nimelrian

Background: 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 FormidableLabs/radium#907

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions