Nightly Procrastination Machine

    react-required-if

    1.0.3 • Public • Published

    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')
    );

    Keywords

    none

    Install

    npm i react-required-if

    DownloadsWeekly Downloads

    75,749

    Version

    1.0.3

    License

    MIT

    Last publish

    Collaborators

    • thejameskyle