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 4 Basics Navigating, Nesting and Redirecting Routes Using the Match Object

Cannot read property 'url' of undefined when using match.url?

When using Guil's code line by line (even copied and pasted from download files) I get TypeError: Cannot read property 'url' of undefined > <li><NavLink to={`${match.url}/html`}>HTML</NavLink></li>

Am I missing something? Or is there an updated solution to this?

3 Answers

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 Points

I know I'm late on this one, but I'm pretty sure this is a failure to destructure match from the values object as a parameter for our Courses function like so:

Courses.js

const Courses = ({ match }) => (
  <div className="main-content courses">
 //etc, etc
}
Ryan Agatep
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Ryan Agatep
Full Stack JavaScript Techdegree Graduate 17,705 Points

I can't get the {match.path} or {match.url} to work with my code. I get the same error message as OP. Any thoughts? Here's my version:

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

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

const Courses = ({match}) => (
  <div className="main-content courses">
    <div className="course-header group">
      <h2>Courses</h2> 
      <ul className="course-nav">
        <li><NavLink to='html'>HTML</NavLink></li>
        <li><NavLink to='css'>CSS</NavLink></li>
        <li><NavLink to='javascript'>JavaScript</NavLink></li>
      </ul>
    </div>

    {/* Write routes here... */}
    <Route exact path="/courses" render={ () => <Redirect to="/courses/html"></Redirect> }></Route>
    <Route path="/courses/html">
      <HTML />
    </Route>
    <Route path="/courses/css">
      <CSS />
    </Route>
    <Route path="/courses/javascript">
      <JavaScript />
    </Route>
  </div>
);

export default Courses;