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 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

28,558 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