React-router is a great addition to React, but yesterday marked the first time I had used it for a project. It led to a period of head scratching, but maybe this post will help someone else avoid the same mistake I made!

Simple Setup

Installing it was simple enough.

npm install --save react-router

Having installed it I then added the import lines,

import {Router, Route, Link, browserHistory} from 'react-router';

and then added some routes to the root component.

render(( ), document.getElementById('test'))

Adding a few links to the components with Link to enable me to test navigation and all should have been good.

About Page

This was intended to give me the following urls,

/ /about /blah

After making the changes a quick refresh of the page (webpack-dev-server really does make life easy) and sure enough I got the page with links. Clicking on the links gave me a surprise though – the URL changed but the page didn’t.

Children?

After some head scratching and a bit of reading around, I discovered that the routing I had added wasn’t quite what I thought in terms of the components. In my minds eye I envisaged this as rendering each component directly, but what I had added actually added was rendering a parent/child relationship.

URLParentChild(ren)
\homenone
\abouthomeabout
\blahhomeblah
Fixing the problem was as simple as changing the **home** component to render the children by adding
{this.props.children}

Nesting

This applies for every level of nesting, so rewriting the routing block to

render(( ), document.getElementById('test'))

Works as expected when displaying /about/about due to using the home component with this.props.children. Using the about component failed as it lacked the this.props.children usage. The relationships look like

URLParentChildChild
\homenonenone
\abouthomehomenone
\about\abouthomehomeabout
\blahhomeblahnone
## Original Intention

In order to get the behaviour I actually wanted I needed to change the routing block to this.

render(( ), document.getElementById('test'))