JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  1. Part 6: Props Starter

6.6: PropTypes

Previous6.5: External Props and Mapping ComponentsNextPart 7: Lifecycle Methods

Last updated 6 years ago

React has built into the environment access to something called PropTypes. PropTypes provide an interesting function for React components. First, they let you control exactly the type of prop passed between components. For instance, our FunctionalComp has props passed to it that we're expecting are strings and functions. If you have developed the FunctionalComp component, you will probably know that the string and function props are supposed to be a string and function, respectively. However, if you come back to this project months later, or if a collaborator is working on this project with you, they may not know the type of data that should be passed through these props. PropTypes will insure that anyone using this FunctionalComp component can't pass the wrong 'type' of data to it.

Second, PropTypes can also provide default values for props if values are failed to be passed to the component. This can both save time if you're going to pass the same type of value over and over, and it can also make sure your component doesn't just flatout break when props are forgotten. Let's investigate these 2 cases below.

Let's begin by importing PropTypes into our PropsDemo.js file. Let's add it directly below the line 1 React import.

import PropTypes from 'prop-types';

Next, let's go ahead and add some default props to the bottom of our file. These will be default props for our FunctionalComp component:

FunctionalComp.defaultProps = {
    string: 'this is a crazy thingamajigger!',
    function: () => console.log('PropTypes are crazy!')
}

Once you view your page, nothing will have changed. Do you know why? DefaultProps exist for when props are not provided. However, we're still specifying our props from inside the .map within our render! Let's comment out the string and function props and see what our page becomes!

If you view your work in Chrome, you should see the following display:

You'll probably notice that the buttons are no longer controlling the DOM as they were before. That's because while our string default prop refers to the value this is a crazy thingamajigger!, the function prop refers to the arrow function which logs to the console. If you click your buttons a few times, you should see something like the following in your console:

So we've seen what defaultProps can do for us, let's now check out PropTypes. Let's add the following code below our defaultProps block:

FunctionalComp.propTypes = {
    string: PropTypes.string.isRequired,
    function: PropTypes.func.isRequired
}

First, you should see that your PropTypes import is now being used in line 2. If you comment out your defaultProps section and view your app, you should see the following in the console:

React is kind enough to throw a warning because your string and function prop types have been omitted, and it's looking for them. You can imagine how including PropTypes in this way will help other developers unfamiliar with the components you're building out to be aware and use your code correctly!

Let's go ahead and un-comment the string and function props. Your app should now be working like normal again, with all of the buttons triggering different features of the DOM, and each <p> specifying what the button does.

If you're interested in learning more about PropTypes, you should check out the official docs on PropTypes here:

https://reactjs.org/docs/typechecking-with-proptypes.html
Default Props View
Default Props Button Clicks
Prop Types Error