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