import { Route, IndexRoute, Link } from 'react-router';从'./App'导入应用程序;从../../pages/home/page"导入主页;从../../pages/about/page"导入关于页面;const Child = ({ match }) =>(<div><h3>ID:{match.params.id}</h3>
I'm unable to get the URL parameter feature in React Router to work, any insight would be helpful! I tried using the 'react-router-dom' instead of 'react-router' but I get the 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 renders the index.htmlas is with no react. Is Express interfering with React? In Express all i have is the following:
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 a different file (left out all the imports and lifecycle functions):
export default React.createClass({
render: function() {
{* bunch of JSX goes here *}
}
});
The html doesn't have the bundle as a script tag because of my webpack config (the bundle is injected!)(the following code is just a piece of the config):
When you add new routes to your app, you can't rely on hot reloading to inject the new route + components. I had to CTRL+C and run npm start again.
This solves the issue!!
Also react-router-dom is a React Router v4 package while react-router is an older version. Thanks to @HalCarleton for pointing that out. I saw code examples everywhere with react-router while the official documentation showed react-router-dom and I was really confused as to why there were two different types of packages.