I have a working shared component library that I import into my front end app as a node_module using package.json.
I have built a PaginationLinks component that uses NavLink:
PaginationLinks.js (in shared component library)
import { NavLink } from 'react-router-dom';
const PaginationLinks = {
return (
<Container>
...
<NavLink to={"/"}>Home</NavLink>
</Container>
);
};
export default PaginationLinks;
In my front end app, I have react-router and all of that set up already in my working app:
routes.js
import { Route, Switch, Redirect } from 'react-router';
import React from 'react';
import { history } from './redux/store';
import { ConnectedRouter } from 'connected-react-router';
...
function App() {
return (
<ConnectedRouter history={history}>
<Switch>
<BasePage>
<Switch>
<Route
exact
path="/"
component={HomePage}
/>
...
</Switch>
</BasePage>
</Switch>
</ConnectedRouter>
);
}
FYI this is already an app where I have existing routes and import NavLink with no problem.
However, when I try to import my component into my app, it is complaining that my NavLink is not inside a router:
Error: Invariant failed: You should not use <NavLink> outside a <Router>
import React from 'react';
import { NavLink } from 'react-router-dom';
import PaginationLinks from '@my-lib/ui/components/PaginationLinks';
import H1 from '@my-lib/ui/components/H1';
const HomePage = () => (
<div>
<H1>hello</H1> // this works
<NavLink to="/somewhere">somewhere</NavLink> // this works
<PaginationLinks /> // this breaks the app
</div>;
)
export default HomePage;
How would I use NavLink inside a shared component library? It doesn't make sense to wrap PaginationLinks inside of a Router in my shared component library.