Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Basics (retired) Thinking in Components Decomposing our Application

Bryan LeBlanc
Bryan LeBlanc
2,482 Points

PropTypes seems to be optional?

It appears that using this code is completely optional because the program still works without it. Could someone explain its purpose?

Application.propTypes = { title: React.PropTypes.string, }

1 Answer

andren
andren
28,523 Points

propTypes exists for type checking purposes, that is to say that it makes it so you'll get a warning if the prop ends up containing a value of a type other than the one you specified it should have. The purpose of that is that it makes it easier to catch bugs.

If you know that a prop should always be a string and you write code that expects that to be the case, then if you at some point end up with something other than a string stored in said prop then that clearly indicates that something is wrong somewhere in your code. The proptype warnings makes it easier to detect issues like that.

For small simple projects like the ones usually shown off in Treehouse tutorials you usually don't benefit greatly from additional type checking and warnings, but in large complex apps where it can be difficult to manually test each section of an app they are very useful for debugging purposes