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

Babak Bandpey
PLUS
Babak Bandpey
Courses Plus Student 7,930 Points

'match' is undefined, why?

const Courses = ({match}) => .....

the match is undefined. Any idea?

Thanks

6 Answers

Babak Bandpey
PLUS
Babak Bandpey
Courses Plus Student 7,930 Points

The problem occurs when i use

<Route path="/courses" render={()=><Courses />} />

instead of

<Route path="/courses" component={Courses} />

I am not sure why render does not pass the props further to the component and I am not sure where those props originate from.

Dan Weru
Dan Weru
47,649 Points

Make sure that you have imported the relevant library. Two makes sure that you interpolate the variables where necessary.

// imports
import { Route, NavLink, Redirect } from 'react-router-dom';
...
const Courses = ({match}) => ...

<navLink to {`${match.url}`} ... 

My bet is that the error has something to do with interpolation. A look at your workspace or your code could help.

Babak Bandpey
Babak Bandpey
Courses Plus Student 7,930 Points

Hi Without the curly braces it becomes an empty object It is regarding this course: https://teamtreehouse.com/library/using-the-match-object

Babak Bandpey
Babak Bandpey
Courses Plus Student 7,930 Points

Thank you for your reply

This is the code

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

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


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

      <Switch>
        <Route exact path={match.path} render={()=><Redirect to={`${match.path}/html`} />} />
        <Route path={`${match.path}/html`} component={HTML} />
        <Route path={`${match.path}/css`} component={CSS} />
        <Route path={`${match.path}/javascript`} component={JavaScript} />
        <Route component={NotFound} />
      </Switch>
    </div>
  );
}

export default Courses;

I have imported all the related libs, I just did some improvisations and used render instead of the component and the code didn't work anymore.

I'll appreciate any feedback.

Dan Weru
Dan Weru
47,649 Points

Hey, why did you feel the need to improvise? Using the component would have been fine. It seems to me that you probably have a syntax error in one of your templates. Can you provide me a link to your workspace?

Dan Weru
Dan Weru
47,649 Points

Hi,

I see, could you push your project to a github/gitlab repo? If yes, kindly share the repo link, I will certainly look into it. Thanks

Dan Weru
Dan Weru
47,649 Points

Great. I pulled your repo, did an npm install and then run npm start. You code seems to work flawlessly, everything is rendering fine. All the routes are working so far. Ensure that you have create-react-app installed globally. Then also run an npm install command at the root of your project

npm install -g create-react-app

The only error that appears in the console is a warning saying

There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.

Let me know if you're experiencing any other problems

Babak Bandpey
PLUS
Babak Bandpey
Courses Plus Student 7,930 Points

Hi there,

Thanks again. The assignment is for local machine development and there is no workspace link. There are no errors in the console neither.

Babak Bandpey
PLUS
Babak Bandpey
Courses Plus Student 7,930 Points

Dear One,

Here is the link. Just pushed the project to the github https://github.com/babakbandpey/react-router

Please don't use much of your time. I appreciate your kindness and help. sincerely Babak

Babak Bandpey
PLUS
Babak Bandpey
Courses Plus Student 7,930 Points

Hi :)

Very kind of you that you took the time to examine the code.

The issue is still relevant since the usage of:

<Route path="/teachers/:fname-:lname" render="{()=><Featured />}" />

vs. usage of this

<Route path="/teachers/:fname-:lname" component={Featured} />

would result in not receiving the "match" prop and in general will not receive the props at all.

The reason that I chose to use render over component was just coincidental and led to this discussion thread.

Have a nice Friday

Sincerely Babak

Anyone have a solution for this? I lose access to match when using inline rendering as Babak stated above. If i use the component prop I cannot pass the state to my child component which i need to do. Stuck and can't figure out how to have access to match AND props.