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 Going Further with Routing Navigating Routes Programmatically

Can't declare function in react component

I get an error saying unrecognized token on the var keyword. Does anybody know why?

1 Answer

Chris Martinez
Chris Martinez
11,715 Points

Without code, I can't say for certain if this is the answer, but from from previous experience, my guess is that you are trying to assign a variable in the context of declaring a class-based component. Ex:

class Home extends Component {
  someAction = (e) =>{
 }
  someProp: 'peanuts';
 anotherProp: 'cola';

}

anything you write between the first and last curly brackets you are in actuality writing in ES6 style object notation, so someProp and someAction are actually a property and a method, respectively, and we never use the "var/let/const" keywords in this name space, since everything is written directly to the object.

In the case of this of the Featured component, we are writing a Functional Component

const Featured = ({match}) => {
  var name = match.params.name;
  var topic = match.params.topic;
 var someFunc = ()=>{
......
}

}

This is really like any other normal function in JS, and anything in between is actually a block level variable. You're not writing to a "Featured" Object, you're creating a block scope.

Hope this helps!