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

Brett Connolly
Brett Connolly
12,874 Points

When to make a React component a class or a simple object?

In the React Redux tutorials, when creating separate components to handle different functionality of the scoreboard, some of the components are defined inside a class, like 'AddPlayerForm' and others are simple functions stored in a 'const' variable inside their own files. Any more clarification as to why they are defined differently (class vs const variable)?

1 Answer

Stan Day
Stan Day
36,802 Points

Currently while everything is still being compiled to es5 with babel there is no advantage of using a stateless function over a class although many people prefer to use them when possible as its quite clean syntax. You cannot use a stateless function and must use a class if you need to use any of the lifecycle methods or if you need to store state in your component. In the future when es6 is implemented natively it may be more performant to use stateless functions instead of classes where possible.