Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
assign-prop-types
Advanced tools
Readme
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>
));
Yarn:
yarn add assign-prop-types
Npm:
npm install assign-prop-types --S
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';
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;
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>));
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);
Target component will be mutated. Keep this fact in your mind.
Vladimir Kalmykov vladimirmorulus@gmail.com
MIT, 2017
FAQs
Assign propTypes (and defaultProps & contextTypes) to the component in a functional style
The npm package assign-prop-types receives a total of 17 weekly downloads. As such, assign-prop-types popularity was classified as not popular.
We found that assign-prop-types demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.