React Pattern: Centralized PropTypes

Avoid repeating yourself by centralizing PropTypes

Image for post
Image for post
Grand Central Station, New York, NY

There are three popular ways to handle types in React: PropTypes, TypeScript and Flow. This post is about PropTypes, which are currently the most popular.

Since PropTypes provide type warnings at runtime, it’s helpful to be as specific as possible.

  • Component accepts an object? Declare the object’s shape.
  • Prop only accepts a specific list of values? Use oneOf.
  • Array should contain numbers? Use arrayOf.
  • You can even declare your own types. AirBnB offers many additional PropTypes.

Here’s a PropType example:

PropType declaration example

In real apps with large objects, this quickly leads to a lot of code. That’s a problem, because in React, you’ll often pass the same object to multiple components. Repeating these details in multiple component files breaks the DRY principle (don’t repeat yourself). Repeating yourself creates a maintenance problem.

The solution? Centralize your PropTypes.

I prefer centralizing PropTypes in /types/index.js.

I’m using named imports on line 2 to shorten the declarations. 👍

And here’s how I use the PropType I declared above:

I use a named import to get a reference to the exported PropType declaration on line 2. And I put it to use on line 13.


  1. The centralized PropType radically simplifies the component’s PropType declaration. Line 13 just references the centralized PropType, so it’s easy to read.
  2. The centralized type only declares the shape, so you can still mark the prop as required as needed.
  3. No more copy/paste. If the object shape changes later, you have a single place to update. 🎉

Here’s a working example on CodeSandbox.

Finally, consider writing some custom code to generate your PropType declarations from your server-side code. For example, if your API is written using a strongly typed language like C# or Java, consider generating your PropType declarations as part of your server-side API build process by reading the shape of your server-side classes. This way you don’t have to worry about keeping your client-side PropTypes and your server-side API code in sync. 👍

Side-note: If you know of a project that does this for any server-side languages, please reply in the comments and I’ll add a link here.

Edit: You can convert JSON into PropTypes using transform.now.sh. 🎉


  1. Declare your PropTypes as explicitly as possible, so you know when you’ve made a mistake.
  2. Centralize your PropTypes to avoid repeating yourself.
  3. If you’re working in a strongly typed language on the server, consider generating your PropTypes by reading your server-side code. This assures your PropTypes match your server-side types.

Looking for More on React? ⚛️

I’ve authored multiple React and JavaScript courses on Pluralsight (free trial).

Image for post

Cory House is the author of multiple courses on JavaScript, React, clean code, .NET, and more on Pluralsight. He is principal consultant at reactjsconsulting.com, a Software Architect at VinSolutions, a Microsoft MVP, and trains software developers internationally on software practices like front-end development and clean coding. Cory tweets about JavaScript and front-end development on Twitter as @housecor.

Written by

Pluralsight Author, Principal at reactjsconsulting.com, Software Architect, Microsoft MVP, Speaker, Clean Coder, Aspiring Outlier.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store