Additional React PropTypes Validators

0.4.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates the setup and usage of various custom PropTypes like `elementType`, `all`, and `deprecated` within a React component.

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;

view raw JSON →