Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

assign-prop-types

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assign-prop-types

Assign propTypes (and defaultProps & contextTypes) to the component in a functional style

  • 1.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
decreased by-9.09%
Maintainers
1
Weekly downloads
 
Created
Source

assign-prop-types

Allows you to create stateless React components with assigned propTypes (and defaultProps & contextTypes) without breaking the chain.

export default assignPropTypes(
  // propTypes
  {
    selector: PropTypes.func,
  },
  // defaultProps
  {
    selector: () => 'No selector passed'
  },
  // contextTypes
  {
    store: PropTypes.object
  }
)(({ selector }, { store }) => (
  <div>{selector(store.getState())}</div>
));

Install

Yarn:

yarn add assign-prop-types

Npm:

npm install assign-prop-types --S

Import

import assignPropTypes from 'assign-prop-types';

In most cases, you will also need to import packages react and prop-types (or React.PropTypes for React < v15.5).

import React from 'react';
import PropTypes from 'prop-types';
import assignPropTypes from 'assign-prop-types';

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.

export default assignPropTypes({
  children: PropTypes.node.isRequired,
})(({ children }) => (<div>{children}</div>));

Identical to:

const Component = ({ children }) => (<div>{children}</div>);
Component.propTypes = {
  children: PropTypes.node.isRequired,
};
export default Component;

Reusable assigner

Assigners can be prepared in advance:

const assignUsualTypes = assignPropTypes({
  children: PropTypes.node,
}, {
  children: "No children specified"
});

And applied later multiple times:

export const H1 = assignUsualTypes(({ children }) => (<h1>{children}</h1>));
export const H2 = assignUsualTypes(({ children }) => (<h2>{children}</h2>));

Extending

Assigners can be extended. To extend assigner, just call it with advanced configuration:

const usualPropTypes = assignPropTypes({
  children: PropTypes.node.isRequired,
});
export default usualPropTypes({
  title: PropTypes.string,
})(YourComponent);

// propTypes will be { children, title }

Or by passing another assigner(s) to combine them:

import assignerA from './assignerA';
import assignerB from './assignerB';
import assignerC from './assignerC';

export default assignPropTypes(
  assignerA,
  assignerB,
  assignerC
)(YourComponent);

👾 Mutates!

Target component will be mutated. Keep this fact in your mind.

Author

Vladimir Kalmykov vladimirmorulus@gmail.com

License

MIT, 2017

Keywords

FAQs

Package last updated on 20 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc