https://github.com/jamiebuilds/react-required-if
React PropType to conditionally add `.isRequired` based on other props
https://github.com/jamiebuilds/react-required-if
prop-types props react validation
Last synced: 11 months ago
JSON representation
React PropType to conditionally add `.isRequired` based on other props
- Host: GitHub
- URL: https://github.com/jamiebuilds/react-required-if
- Owner: jamiebuilds
- License: mit
- Created: 2016-04-06T02:27:55.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-07-01T08:29:09.000Z (almost 5 years ago)
- Last Synced: 2025-03-12T21:02:50.731Z (about 1 year ago)
- Topics: prop-types, props, react, validation
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 73
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-required-if
> React PropType to conditionally add `.isRequired` based on other props
## Install
```sh
$ npm install --save react-required-if
```
## Usage
```js
import React, {PropTypes} from 'react';
import requiredIf from 'react-required-if';
export default class Component extends React.Component {
static propTypes = {
disabled: PropTypes.bool,
onClick: requiredIf(PropTypes.func, props => !props.disabled)
};
render() {
return Click Me
}
}
```
**Result:**
```js
import React from 'react';
import {render} from 'react-dom';
import Component from './Component';
render(
{})/> // ok
// ok
// NOooooooo
,
document.getElementById('root')
);
```