Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript React Router Basics Navigating and Nesting Routes Index Routes and Redirects

IndexRoute does not work

Hi, IndexRoute does not work for me.

Here is my router code.

import React from 'react'
import { render } from 'react-dom'
import {Router, Route, IndexRoute, browserHistory} from 'react-router'

// Components
import App from './components/App'
import Home from './components/Home'
import About from './components/About'
import Teachers from './components/Teachers'
import Courses from './components/Courses'

import HTML from './components/courses/HTML'
import CSS from './components/courses/CSS'
import JavaScript from './components/courses/JavaScript'

const routes = (<Router history={browserHistory}>
  <Route component={App}>
    <Route path='/' component={Home} />
    <Route path='about' component={About} />
    <Route path='courses' component={Courses} />
    <Route path='teachers' component={Teachers} />

    <Route path='courses' component={Courses}>
      <IndexRoute component={HTML} />
      <Route path='html' component={HTML} />
      <Route path='css' component={CSS} />
      <Route path='javascript' component={JavaScript} />
    </Route>

  </Route>
</Router>)

export default routes

Can anyone spot the errors? Same problem with IndexRedirect: /courses just displays the buttons (however the routes are workign)

Kristóf Dombi
Kristóf Dombi
28,047 Points

Hey nope!

Quick fix maybe: Try using Redirect component instead of IndexRoute.

If that didn't work: I guess you use the older version of react-router, i use the newer ones, so lots of stuff was different from the videos. I hat to read the manual to get this working. Anyways, here is my solution:

router.js

// Libs
import React from 'react';
import { BrowserRouter as Router, Route, IndexRoute } from 'react-router-dom';

// Components
import App from './components/App';
import Home from './components/Home';
import About from './components/About';
import Courses from './components/Courses';
import Teachers from './components/Teachers';

// Routes
const routes = (
  <Router>
    <App>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/courses" component={Courses} />
      <Route path="/teachers" component={Teachers} />
    </App>
  </Router>
);

export default routes;

App.js

import React, { Component } from 'react';
import {NavLink} from 'react-router-dom';

class App extends Component {
  render() {
    return (
      <div className="container">
        <header>
          <span className="icn-logo"><i className="material-icons">code</i></span>
          <ul className="main-nav">
            <li><NavLink to="/">Home</NavLink></li>
            <li><NavLink to="/about">About</NavLink></li>
            <li><NavLink to="/teachers">Teachers</NavLink></li>
            <li><NavLink to="/courses">Courses</NavLink></li>
          </ul>
        </header>
        { this.props.children }
      </div>
    );
  }
}

export default App;

Courses.js

import React, { Component } from 'react';
import { NavLink, Route, Redirect } from 'react-router-dom';

import html from './courses/HTML';
import css from './courses/CSS';
import JavaScript from './courses/JavaScript';

class Courses extends Component {
  render() {
    return (
      <div className="main-content courses">
        <div className="course-header group">
          <h2>Courses</h2>
          <ul className="course-nav">
            <li><NavLink to='/courses/html'>HTML</NavLink></li>
            <li><NavLink to='/courses/css'>CSS</NavLink></li>
            <li><NavLink to='/courses/javascript'>JavaScript</NavLink></li>
          </ul>
        </div>
        <Redirect from="/" to="/courses/html" />
        <Route path="/courses/html" component={ html } />
        <Route path="/courses/css" component={ css } />
        <Route path="/courses/javascript" component={ JavaScript } />
        { this.props.children }
      </div>
    );
  }
};

export default Courses;

Hope I could help! :)