Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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} />


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:


// 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 = (
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/courses" component={Courses} />
      <Route path="/teachers" component={Teachers} />

export default routes;


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

class App extends Component {
  render() {
    return (
      <div className="container">
          <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>
        { this.props.children }

export default App;


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">
          <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>
        <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 }

export default Courses;

Hope I could help! :)