React Prop Types

15.8.1 · maintenance · verified Sun Apr 19

prop-types is a JavaScript library providing runtime type checking for React component props and similar object structures. It enables developers to define the expected types, shapes, and requirements for properties, issuing helpful warnings in development environments when mismatches occur. Its current stable version is 15.8.1. While once integrated directly into React, it was externalized in React v15.5 to encourage the adoption of static type checking solutions like TypeScript or Flow. prop-types continues to be maintained for projects that prefer runtime validation or are not yet using static typing, serving as a robust, albeit runtime-only, solution for ensuring data integrity within component APIs. It offers a comprehensive set of validators for primitives, instances, enums, arrays, objects, and more, making it flexible for various use cases. The library generally follows a stable release cadence, primarily for maintenance and compatibility updates rather than new features, given its mature state and the ecosystem's shift towards compile-time type checking.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart defines a React component `MyComponent` and illustrates the declaration of various prop types using `prop-types`, including primitive types, `instanceOf`, `oneOf`, `oneOfType`, `arrayOf`, `objectOf`, `shape`, and `exact` validators. It also shows chaining `isRequired`.

import React from 'react';
import PropTypes from 'prop-types';

class MyMessage {
  constructor(text) {
    this.text = text;
  }
}

class MyComponent extends React.Component {
  render() {
    // Example of using props inside render
    return (
      <div>
        <p>String: {this.props.optionalString}</p>
        <p>Number: {this.props.optionalNumber}</p>
        <p>Enum: {this.props.optionalEnum}</p>
        <p>Message Text: {this.props.optionalMessage ? this.props.optionalMessage.text : 'N/A'}</p>
        <p>Required Property: {this.props.optionalObjectWithShape.requiredProperty}</p>
      </div>
    );
  }
}

MyComponent.propTypes = {
  optionalArray: PropTypes.array,
  optionalBool: PropTypes.bool,
  optionalFunc: PropTypes.func,
  optionalNumber: PropTypes.number,
  optionalObject: PropTypes.object,
  optionalString: PropTypes.string,
  optionalNode: PropTypes.node,
  optionalElement: PropTypes.element,
  optionalElementType: PropTypes.elementType,
  optionalMessage: PropTypes.instanceOf(MyMessage),
  optionalEnum: PropTypes.oneOf(['News', 'Photos']).isRequired,
  optionalUnion: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.instanceOf(MyMessage)
  ]),
  optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
  optionalObjectOf: PropTypes.objectOf(PropTypes.number),
  optionalObjectWithShape: PropTypes.shape({
    optionalProperty: PropTypes.string,
    requiredProperty: PropTypes.number.isRequired
  }).isRequired,
  // A required object with an exact set of properties and types
  // This will warn if extra properties are supplied, unlike .shape
  strictObject: PropTypes.exact({
    a: PropTypes.number,
    b: PropTypes.string.isRequired
  })
};

// Example usage (in a React application)
// function App() {
//   return (
//     <MyComponent
//       optionalString="hello"
//       optionalNumber={123}
//       optionalEnum="News"
//       optionalMessage={new MyMessage('Hello from PropType!')}
//       optionalObjectWithShape={{ requiredProperty: 42 }}
//       strictObject={{ a: 1, b: 'two' }}
//     />
//   );
// }
// export default App;

// To prevent 'React' is defined but never used, mock a render if not in a React app context
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') {
  console.log('PropTypes example component defined. Run in a React app for full effect.');
}

view raw JSON →