1.0.1 • Published 4 years ago

prop-types-nullable v1.0.1

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

Prop-Types Nullable

Small convenience function for prop-types which allows explicitly null props but rejects omissions.

Install dependency

npm install --save prop-types-nullable

Usage

This builds on top of prop-types, so see that project's documentation for the basics of the API.

Simple usage:

const nullable = require('prop-types-nullable');

MyReactClass.propTypes = {
  foobar: nullable(PropTypes.func).isRequired,
};

// foobar is now a required property, but can be explicitly null

It is possible to use nullable without isRequired, but this is pointless (as null will be allowed for a non-required property anyway). This project maintains the need for isRequired for consistency with the prop-types API, and to better integrate with linting rules.

See https://github.com/facebook/react/issues/3163 for a discussion around adding similar functionality to the core prop-types project.