I am using react-router-dom
and react-router-redux
for routing in React. I am unable to route to the relative path. In my case '/login.
Here is the code. https://codesandbox.io/s/y73931q871
Also if I run the code in the local it throws:
I am using react-router-dom
and react-router-redux
for routing in React. I am unable to route to the relative path. In my case '/login.
Here is the code. https://codesandbox.io/s/y73931q871
Also if I run the code in the local it throws:
In webpack.config.js, you should not miss historyApiFallback: true
for the devServer object. Something like this:
devServer: {
compress: true,
port: 9000,
host: '0.0.0.0',
historyApiFallback: true
},
Also, in your Route configuration, you need to reorder the Routes like
export const App = () => (
<Provider store={store}>
<Router history={history}>
<Switch>
<Route path="/login" component={Login} />
<Route path="/" component={Home} />
</Switch>
</Router>
</Provider>
);
re-order
is needed because Switch
only renders the first matched route and a path like /login
will also match /
and hence only render Home
all the time, so all paths that are prefixed to other paths need to be placed at the end.
According to docs, you should use exact
prop with index (/
) route.
So the code should looks like this:
export const App = () => (
<Provider store={store}>
<Router history={history}>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
</Switch>
</Router>
</Provider>
);
Please modify your home route by the following and it will work as you want
<Route path="/" component={Home} exact />