Tutorial

Working With Types Using React.PropType

Published on February 13, 2017
author

Casey A. Childers

Working With Types Using React.PropType

As compilation (er…transpilation) becomes the defacto standard of the post ES5, JSX-leaning JS dev landscape—care of tools like Babel—there is a growing sentiment that it may be time to let go of our fair language’s dynamic and coercive ways in favor of a more sensible, statically-typed way of doing things.

Which leads to questions like, “Sure, but where do I even start?”

The biggest camps are TypeScript (Microsoft) and Flow (Facebook), but if you’re working in React you’ve already got a wonderful, low-maintenance alternative in your codebase that’s as easy to implement as it is to learn.

import React, { PropTypes } from 'react';

Say hello to React.PropTypes, a developer-friendly tool for declaring and checking types. Sure, it handles all the primitives ( array, bool, func, number, object, string, symbol) easily enough:

function Nest({name}) {
  return (
    <div>
      We called it {name}.
    </div>
  );
}

Nest.propTypes = {
  name: PropTypes.string,
}

export default Nest;

But there’s also enough depth under the hood to manage some pretty sophisticated verifications:

Nest.propTypes = {
  name: PropTypes.string,
  eggs: PropTypes.shape({
    species: PropTypes.string,
    femaleToMale: PropTypes.arrayOf(PropTypes.number),
    hobbies: PropTypes.oneOf(['swimming', 'stalking']),
  }),
}

All-in you get nine additional types to throw your props against:

  • node and element for all your DOM and React element needs
  • instanceOf for digging into prototypes
  • oneOf for lists of known potential values
  • oneOfType for lists of known potential types
  • arrayOf and objectOf for arrays and objects composed of a single type
  • shape for objects in the shape of a specific schema
  • and any for when you really don’t want to check for a type

Furthermore, you can attach isRequired to any PropType declaration for added rigidity.

hobbies: PropTypes.oneOf(['swimming', 'stalking']).isRequired

Couple things to note, PropType checking is disabled in production, and it’s only present as a guiding hand in development mode. It throws warnings, not errors.

A missing required prop warning in the Chrome console

👉 That’s it. You’re a type checking expert now. Don’t abuse this new power in opinion convos.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Casey A. Childers

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.