I've already spent half a day on this issue and can't find what is wrong. I use react-router on server. But for every route, it renders the same component (the one from root route).
Here is my server:
//routes
import routes from "../shared/routes";
app.get('*', (request, response) => {
match({ routes: routes, location: request.url }, (err, redirect, props) => {
if (err) {
response.status(500).send(err.message)
} else if (redirect) {
response.status(302).redirect(redirect.pathname + redirect.search)
} else if (props) {
console.log('Rendering '+JSON.stringify(props));
const appHtml = ReactDOMServer.renderToString(<RouterContext {...props}/>);
response.render('app', {app: appHtml});
} else {
response.status(404).send('Not Found')
}
});
});
Here is my routes:
export default (
<Route component={AppHandler} path="/">
<IndexRoute component={AppHandler}/>
<Route component={AboutHandler} path="about" />
</Route>
);
Other observations:
{"routes":[{"path":"/","indexRoute":{},"childRoutes":[{"path":"about"}]},{"path":"about"}],"params":{},"location":{"pathname":"/about","search":"","hash":"","action":"POP","key":"bqces8","query":{}},"components":[null,null],"router":{"location":{"pathname":"/about","search":"","hash":"","action":"POP","key":"bqces8","query":{}},"params":{},"routes":[{"path":"/","indexRoute":{},"childRoutes":[{"path":"about"}]},{"path":"about"}]},"matchContext":{"transitionManager":{},"router":{"location":{"pathname":"/about","search":"","hash":"","action":"POP","key":"bqces8","query":{}},"params":{},"routes":[{"path":"/","indexRoute":{},"childRoutes":[{"path":"about"}]},{"path":"about"}]}}}
UPDATE:
AppHandler and AboutHandler are built using webpack. They are imported like:
import AppHandler from '../../build/app';
import AboutHandler from '../../build/about';
Here are relevant parts of both files:
app.js:
var App = function (_Component) {
_inherits(App, _Component);
function App() {
_classCallCheck(this, App);
return _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).apply(this, arguments));
}
_createClass(App, [{
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
null,
'App root'
);
}
}]);
return App;
}(_react.Component);
exports.default = App;
And about.js:
var About = function (_Component) {
_inherits(About, _Component);
function About() {
_classCallCheck(this, About);
return _possibleConstructorReturn(this, (About.__proto__ || Object.getPrototypeOf(About)).apply(this, arguments));
}
_createClass(About, [{
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
null,
'About'
);
}
}]);
return About;
}(_react.Component);
exports.default = About;
I misundrestood nested routes in react-router and missed {this.props.children}
in AppHandler.