Invalid Router URL Parameters
I cannot get the url parameter function in React Router to work, any understanding would be helpful! I tried using "react-router-dom" instead of "react-router" but I am getting errors this guy is getting: https://github.com/ReactTraining/react-router/issues/1799
When I do localhost: 8000 the app works. When I go to localhost: 8000/123, the browser displays index.html as it is unresponsive. Is Express interfering with response? In Express, all I have is this:
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
Here's my main.js :
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import Routes from './shared/components/Routes';
const ROOT_ELEMENT = 'app';
ReactDOM.render((
<Router history={browserHistory}>
{Routes}
</Router>
), document.getElementById(ROOT_ELEMENT));
My routes.js :
import { Route, IndexRoute, Link } from 'react-router';
import App from './App';
import HomePage from '../../pages/home/page';
import AboutPage from '../../pages/about/page';
const Child = ({ match }) => (
<div>
<h3>ID: {match.params.id}</h3>
</div>
)
export default (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
{/* <Route path="about" component={AboutPage} /> */}
<Route path="/:id" component={Child} />
</Route>
);
My Home component is defined like this in another file (not all import and lifecycle functions are specified):
export default React.createClass({
render: function() {
{* bunch of JSX goes here *}
}
});
As requested, my index.html file is :
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Consent Form</title>
<meta name="description" content="Privacy Consent Webform">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--Google Material Design Icons-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<div id="app"></div>
</body>
</html>
There is no package in the html as a script tag due to my webpack config (bundle is inserted!) (The following code is just a part of the config):
entry: [
'webpack-hot-middleware/client',
'bootstrap-loader',
path.join(__dirname, 'src/main.js')
],
output: {
path: path.join(__dirname, '/dist/'),
filename: '[name].js',
publicPath: '/'
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.tpl.html',
inject: 'body',
filename: 'index.html'
})
source to share
When you add new routes to your application, you cannot rely on hot reload to introduce new route + components. I had to CTRL + C and run again npm start
.
This solves the problem!
Also agent-router-dom is a React Router v4 package, while ret-router is an older version. Thanks to @HalCarleton for pointing this out. I saw code examples all over the place with react-router-dom while the official documentation showed react-router-dom and I was very confused as to why there are two different types of packages.
source to share