react-required-if
v1.0.3
Published
React PropType to conditionally add `.isRequired` based on other props
Downloads
258,624
Readme
react-required-if
React PropType to conditionally add
.isRequired
based on other props
Install
$ npm install --save react-required-if
Usage
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 <button onClick={this.props.onClick}>Click Me</button>
}
}
Result:
import React from 'react';
import {render} from 'react-dom';
import Component from './Component';
render(
<div>
<Component onClick={() => {})/> // ok
<Component disabled={true}/> // ok
<Component/> // NOooooooo
</div>,
document.getElementById('root')
);