I use react-router-dom
for routing in my React
application. Part of my app extracted in another package. List of dependencies looks like this:
I solved this problem by changing:
import {Route, Switch} from "react-router";
to
import {Route, Switch} from "react-router-dom";
just add -dom.
I had a similar problem when trying to replace Rails views in a Solidus app.
The solution for me was to edit the vies to use the React Router that I had just added, but apparently hadn't fully configured.
For example, I edited views/spree/home/index.html
from:
<%= javascript_pack_tag 'application' %>
<%= react_component("Home/Index") %>
to:
<%= javascript_pack_tag 'application' %>
<%= react_component("Routes") %>
For more reference, here is my javascript/components/Route.js
:
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import Home from "./Home/Index";
import Product from "./Products/Show";
class Routes extends React.Component {
render() {
return (
<Router>
<Route exact path="/" component={Home} />
<Route path="/products/:slug" component={Product} />
</Router>
);
};
};
export default Routes;
I solve this error, by wrapping my parent component inside Router.
Before solving error
<Header />
<div className="col-md-12 pd-0-0">
<div className="row">
<Sidebar />
<div className="col-sm-10 col-md-10 pd-0-0" style={style}>
<Router>
<Switch>
<Route path="/dashboard/check-in" component={CheckIn} />
<Route path="/dashboard/deals" component={Deals} />
<Route path="/dashboard/events" component={Events} />
<Route path="/dashboard/invoice" component={Invoice} />
<Route path="/dashboard/notification" component={Notification} />
<Route path="/dashboard/profile" component={Profile} />
<Route path="/dashboard/redemption" component={Redemptions} />
<Route path="/dashboard/restriction-management" component={RestrictionManagement} />
</Switch>
</Router>
</div>
</div>
</div>
After solving error
<Router>
<Header />
<div className="col-md-12 pd-0-0">
<div className="row">
<Sidebar />
<div className="col-sm-10 col-md-10 pd-0-0" style={style}>
<Switch>
<Route path="/dashboard/check-in" component={CheckIn} />
<Route path="/dashboard/deals" component={Deals} />
<Route path="/dashboard/events" component={Events} />
<Route path="/dashboard/invoice" component={Invoice} />
<Route path="/dashboard/notification" component={Notification} />
<Route path="/dashboard/profile" component={Profile} />
<Route path="/dashboard/redemption" component={Redemptions} />
<Route path="/dashboard/restriction-management" component={RestrictionManagement} />
</Switch>
</div>
</div>
</div>
</Router>
Actually the issue was in your root component where you are doing routing, you should not add any component outside of Router
tag in the component, that component only expect single/multiple component wraped by Route
under Router
in that components.
wrong code (but it will run, but you are creating bug) -
function App() {
return (
<div className="App">
<Header/>
<Router>
<Switch>
<Route path="/" exact>
<Home />
</Route>
</Switch>
</Router>
</div>
);
}
I only remove <Header/>
, it solved the issue.
write code -
function App() {
return (
<div className="App">
<Router>
<Switch>
<Route path="/" exact>
<Home />
</Route>
</Switch>
</Router>
</div>
);
}
You need import the named export Router
as well from react-router-dom
and wrap it around your Switch/Route components.
const App = () => (
<Router>
<Suspense fallback={<Placeholder />}>
<Switch>
<Route path="/auth" component={Auth} />
<Route path="/" component={Index} />
</Switch>
</Suspense>
</Router>
);
I got this during testing my component which linked to the project (using npm link
) and because of that react-router-dom
loaded more than once, following solution works in my case:
Inside webpack.config.js
I added:
resolve: {
alias: {
'react-router-dom': path.resolve('./node_modules/react-router-dom')
}
}