
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
react-proptype-conditional-require
Advanced tools
Conditionally require a React proptype based on other props and/or other conditions.
Conditionally require propTypes based on other props and variables.
$ npm install --save react-proptype-conditional-require
import React, { PropTypes } from 'react';
import isRequiredIf from 'react-proptype-conditional-require';
const Hello = props => <div className={props.className}>Hello {props.value}!</div>;
Hello.defaultProps = {
value: 'World'
};
const { string } = PropTypes;
Hello.propTypes = {
value: string
className: isRequiredIf(string, (props, propName, componentName) => props.hasOwnProperty('value'))
};
This is a function that accepts a propType (a typeValidator function) and a condition in which to enforce this propType for React components. The function uses the signature:
import isRequiredIf from 'react-proptype-conditional-require';
...
Component.propTypes = {
foo: isRequiredIf(typeValidator, conditional[, message])
}
A function that takes the arguments (props, propName, componentName) and returns an Error object if the validation fails. Do not console.warn or throw.
NOTE: All of the React built-in proptypes use this signature and you will usually use them to specify the typeValidator:
import React, { PropTypes } from 'react';
import isRequiredIf from 'react-proptype-conditional-require';
const { string, bool } = PropTypes;
...
Component.propTypes = {
first: isRequiredIf(string, true),
second: isRequiredIf(bool, false)
}
...
A boolean or function that returns a truthy value that indicates whether the prop is required or not. The function follows the same signature as the typeValidator function: (props, propName, componentName). It should return a boolean, but any truthy value will do.
A common use case:
import React, { PropTypes } from 'react';
import isRequiredIf from 'react-proptype-conditional-require';
const { string } = PropTypes;
...
Component.propTypes = {
label: string,
labelClassName: isRequiredIf(string, props => props.hasOwnProperty('label'));
}
...
In that case, the labelClassName will only be required if label is passed.
A string that specifies the custom error message that you would like to provide if the prop is required but missing. If not provided, the error message used by React will be default.
import React, { PropTypes } from 'react';
import isRequiredIf from 'react-proptype-conditional-require';
const { string } = PropTypes;
...
Component.LABEL_CLASSNAME_ERROR_MESSAGE = 'You must provide a labelClassName when passing down the label prop';
Component.propTypes = {
label: string,
labelClassName: isRequiredIf(string, props => props.hasOwnProperty('label'), Component.LABEL_CLASSNAME_ERROR_MESSAGE);
}
...
The above code snippet would throw the custom error message if the label prop were passed but the labelClassName prop was not.
MIT License.
FAQs
Conditionally require a React proptype based on other props and/or other conditions.
The npm package react-proptype-conditional-require receives a total of 177,366 weekly downloads. As such, react-proptype-conditional-require popularity was classified as popular.
We found that react-proptype-conditional-require demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.