assign-prop-types
Allows you to create stateless React components with assigned propTypes (and defaultProps & contextTypes) without breaking the chain.
// propTypes selector: PropTypesfunc // defaultProps 'No selector passed' // contextTypes store: PropTypesobject <div></div>;
Install
Yarn:
yarn add assign-prop-types
Npm:
npm install assign-prop-types --S
Import
;
In most cases, you will also need to import packages react and prop-types (or React.PropTypes
for React < v15.5).
;;;
Usage
The function assignPropTypes accepts optional parameters [propTypes
], [defaultProp
], [contextTypes
] and returns function, called assigner, which, in turn, accepts React component and returns component, mutaded with passed properties.
children: PropTypesnodeisRequired <div>children</div>;
Identical to:
const Component = <div>children</div>;ComponentpropTypes = children: PropTypesnodeisRequired;;
Reusable assigner
Assigners can be prepared in advance:
const assignUsualTypes = ;
And applied later multiple times:
const H1 = ;const H2 = ;
Extending
Assigners can be extended. To extend assigner, just call it with advanced configuration:
const usualPropTypes = ; title: PropTypesstringYourComponent; // propTypes will be { children, title }
Or by passing another assigner(s) to combine them:
;;; assignerA assignerB assignerCYourComponent;
👾 Mutates!
Target component will be mutated. Keep this fact in your mind.
Author
Vladimir Kalmykov vladimirmorulus@gmail.com
License
MIT, 2017