Additional React PropTypes Validators
The `react-prop-types` library provides a collection of supplementary validators for React's original `PropTypes` system. Introduced to extend the native `React.PropTypes` with utilities such as `elementType`, `all`, `componentOrElement`, `deprecated`, and `isRequiredForA11y`, it addresses common validation patterns not covered by React itself. The current stable version is 0.4.0, which reflects its development primarily during the React 0.14.x to early 15.x era. This package is specifically designed for use with older React applications where `PropTypes` were still part of the `react` package, rather than the separate `prop-types` package introduced with React 15.5.0. Its release cadence is effectively inactive, and it differentiates itself by offering specific, reusable validation functions for accessibility, deprecation warnings, and type checking for React components and DOM elements.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'bool') OR TypeError: Cannot read property 'bool' of undefined
cause This error typically occurs when `React.PropTypes` is accessed in a React version where it has been removed (React 16+) or is expected to be imported from the `prop-types` package (React 15.5.0+). The `react-prop-types` library internally relies on `React.PropTypes`.fixEnsure your project uses an older React version (e.g., <15.5.0) compatible with `React.PropTypes`. For newer React versions, you must either replace `react-prop-types` with alternative validation logic or provide a global `PropTypes` object using the `prop-types` package if you are migrating an older codebase. -
Module not found: Error: Can't resolve 'react-prop-types/lib/elementType' OR Cannot find module 'react-prop-types/lib/elementType'
cause The module path specified in the `import` or `require` statement is incorrect, or the `react-prop-types` package is not installed.fixVerify that `react-prop-types` is installed (`npm i react-prop-types`) and that the import path precisely matches the file structure, e.g., `import elementType from 'react-prop-types/lib/elementType';`.
Warnings
- breaking This library relies on the global `React.PropTypes` object, which was deprecated in React 15.5.0 and removed in React 16. It is not directly compatible with modern React applications (React 15.5.0 and newer) without a compatibility layer like `prop-types` or equivalent shims. New applications should use the official `prop-types` package and implement custom validators if needed.
- gotcha Using named imports from the package root (`import { elementType } from 'react-prop-types';`) might increase bundle size compared to direct imports from the `lib` subdirectory (`import elementType from 'react-prop-types/lib/elementType';`). The library structure is optimized for direct file imports for better tree-shaking.
- deprecated The `deprecated` validator within this library logs a warning to the console if a prop is used that is marked as deprecated. This is a design feature, not a problem with the validator itself, but developers should be aware that their console might show warnings for intended deprecations.
- gotcha As an unmaintained library tied to an older React ecosystem, `react-prop-types` may not receive updates for security vulnerabilities or modern JavaScript/TypeScript compatibility. Using unmaintained dependencies can pose supply chain risks.
Install
-
npm install react-prop-types -
yarn add react-prop-types -
pnpm add react-prop-types
Imports
- elementType
import { elementType } from 'react-prop-types';import elementType from 'react-prop-types/lib/elementType';
- all
import all from 'react-prop-types/lib/all';
import { all } from 'react-prop-types'; - deprecated
const deprecated = require('react-prop-types').deprecated;import deprecated from 'react-prop-types/lib/deprecated';
- componentOrElement
import { componentOrElement } from 'react-prop-types';import componentOrElement from 'react-prop-types/lib/componentOrElement';
Quickstart
import React from 'react';
import PropTypes from 'prop-types'; // Or `React.PropTypes` in older React versions
import elementType from 'react-prop-types/lib/elementType';
import { all } from 'react-prop-types';
import deprecated from 'react-prop-types/lib/deprecated';
const MyComponent = ({ Component, optionalComponent, block, vertical, oldProp }) => (
<div>
<h1>Hello from MyComponent</h1>
{Component && <Component />}
{optionalComponent && React.isValidElement(optionalComponent) && optionalComponent}
<p>Block: {String(block)}, Vertical: {String(vertical)}</p>
{oldProp && <p>Old Prop Value: {oldProp}</p>}
</div>
);
MyComponent.propTypes = {
Component: elementType.isRequired,
optionalComponent: elementType,
vertical: PropTypes.bool.isRequired,
block: all(
PropTypes.bool.isRequired,
({ block, vertical }) => (
block && !vertical ?
new Error('`block` requires `vertical` to be set to have any effect') :
null
)
),
oldProp: deprecated(PropTypes.string, 'Use `newProp` instead of `oldProp`.'),
};
MyComponent.defaultProps = {
block: false,
vertical: false
};
// Example usage (e.g., in an App.js):
// import MyComponent from './MyComponent';
// const AnotherComponent = () => <span>I'm another component!</span>;
// <MyComponent Component={AnotherComponent} vertical block oldProp="deprecated value" />
export default MyComponent;