JSPM

react-required-if

1.0.3
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 80910
    • Score
      100M100P100Q153913F
    • License MIT

    React PropType to conditionally add `.isRequired` based on other props

    Package Exports

    • react-required-if

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-required-if) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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