Skip to content

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

License

Notifications You must be signed in to change notification settings

jamiebuilds/react-required-if

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •