Learn
PropTypes
propTypes

In this lesson, you will learn to use an important React feature called propTypes.

propTypes are useful for two reasons. The first reason is prop validation.

Validation can ensure that your props are doing what they’re supposed to be doing. If props are missing, or if they’re present but they aren’t what you’re expecting, then a warning will print in the console.

This is useful, but reason #2 is arguably more useful: documentation.

Documenting props makes it easier to glance at a file and quickly understand the component class inside. When you have a lot of files, and you will, this can be a huge benefit.

Click Next to learn how to use propTypes!

Instructions

In this video, the Stats component has a propTypes object that requires its comments prop to be a number.

When a string is passed to Stats, an error is raised. When a number is passed to Stats, the prop is accepted and the component updates with the new value.

Folder Icon

Take this course for free

Already have an account?