Socket
Socket
Sign inDemoInstall

prop-types

Package Overview
Dependencies
16
Maintainers
9
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    prop-types

Runtime type checking for React props and similar objects.


Version published
Weekly downloads
23M
increased by1.6%
Maintainers
9
Install size
2.51 MB
Created
Weekly downloads
 

Package description

What is prop-types?

The prop-types package is used for runtime type checking of the props that a React component receives. It helps in documenting the intended types of properties passed to components and catching errors in development if a component receives props of incorrect types.

What are prop-types's main functionalities?

Typechecking with PropTypes

This code demonstrates how to use PropTypes to define type requirements for a React component's props. It specifies that the 'name' prop is required and must be a string, 'age' must be a number, 'onButtonClick' should be a function, 'children' should be a React node, 'style' should be an object with numeric values, and 'items' should be an array of strings.

{"Component.propTypes = { name: PropTypes.string.isRequired, age: PropTypes.number, onButtonClick: PropTypes.func, children: PropTypes.node, style: PropTypes.objectOf(PropTypes.number), items: PropTypes.arrayOf(PropTypes.string) };"}

Default Prop Values

This code provides default values for props in a React component. If 'age', 'onButtonClick', or 'items' are not provided by the parent component, they will default to 30, a no-op function, and an empty array, respectively.

{"Component.defaultProps = { age: 30, onButtonClick: () => {}, items: [] };"}

Custom Validators

This code shows how to define a custom validator for a prop. If the 'customProp' does not match the specified pattern, a validation error will be thrown.

{"Component.propTypes = { customProp: function(props, propName, componentName) { if (!/matchme/.test(props[propName])) { return new Error('Validation failed!'); } } };"}

Other packages similar to prop-types

Changelog

Source

15.5.7

  • Critical Bugfix: Fix an accidental breaking change that caused errors in production when used through React.PropTypes. (@gaearon in #20)
  • Improve the size of production UMD build. (@aweary in 38ba18 and 7882a7)

Readme

Source

prop-types

Runtime type checking for React props and similar objects.

Installation

npm install --save prop-types

Importing

import PropTypes from 'prop-types'; // ES6
var PropTypes = require('prop-types'); // ES5 with npm

If you prefer a <script> tag, you can get it from window.PropTypes global:

<!-- development version -->
<script src="https://unpkg.com/prop-types/prop-types.js"></script>
 
<!-- production version -->
<script src="https://unpkg.com/prop-types/prop-types.min.js"></script>

Difference from React.PropTypes: Don’t Call Validator Functions

When you migrate components to use the standalone prop-types, all validator functions will start throwing an error if you call them directly. This makes sure that nobody relies on them in production code, and it is safe to strip their implementations to optimize the bundle size.

Code like this is still fine:

MyComponent.propTypes = {
  myProp: PropTypes.bool
};

However, code like this will not work with the prop-types package:

// Will not work with `prop-types` package!
var errorOrNull = PropTypes.bool(42, 'myProp', 'MyComponent', 'prop');

It will throw an error:

Calling PropTypes validators directly is not supported by the `prop-types` package.
Use PropTypes.checkPropTypes() to call them.

This is new behavior, and you will only encounter it when you migrate from React.PropTypes to the prop-types package. For the vast majority of components, this doesn’t matter, and if you didn’t see this warning in your components, your code is safe to migrate. This is not a breaking change in React because you are only opting into this change for a component by explicitly changing your imports to use prop-types. If you temporarily need the old behavior, you can keep using React.PropTypes until React 16.

If you absolutely need to trigger the validation manually, call PropTypes.checkPropTypes(). Unlike the validators themselves, this function is safe to call in production, as it will be replaced by an empty function:

// Works with standalone PropTypes
PropTypes.checkPropTypes(MyComponent.propTypes, props, 'prop', 'MyComponent');

You might also see this error if you’re calling a PropTypes validator from your own custom PropTypes validator. In this case, the fix is to make sure that you are passing all of the arguments to the inner function. There is a more in-depth explanation of how to fix it on this page. Alternatively, you can temporarily keep using React.PropTypes until React 16, as it would still only warn in this case.

If you use a bundler like Browserify or Webpack, don’t forget to follow these instructions to correctly bundle your application in development or production mode. Otherwise you’ll ship unnecessary code to your users.

Usage

Refer to the React documentation for more information.

Migrating from React.PropTypes

Check out Migrating from React.PropTypes for details on how to migrate to prop-types from React.PropTypes.

Keywords

FAQs

Last updated on 11 Apr 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc