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 - when to use class & when to just use variable?

I notice in "React by Example" tutorial. That the app component is set as a class

class App extends Component { ...... }

But for other component like guestlist, guest, guestname. They are all just using a file and set it as variable like:

const Guest = props =>
Guest.propTypes = { .... }

It seems like it's doing very similar thing, so what is the difference between using class and variable in React? And are their any special consideration factor when choosing which to use?

2 Answers

Sunny, components declared like "const Guest = props =>" and so on are "functional components" that only have a render method. If you use "class" you get a component that can have its own state, constructor, functions and render method. Tip: if you use class then remember that props need to be written "this.props.myProp", not just "props.myProp" like in a functional component. Hope this helps.

Thanks Thomas! it clears up a good deal of question for me.