A function to get all props that were passed to a react component which are not specified as a prop type
MIT License
A dependencies free utility package for getting all extra passed props to an component.
yarn add react-get-not-declared-props
import getNotDeclaredProps from 'react';
const additionalProps = getNotDeclaredProps(props, component, additionalProps);
This function takes three arguments:
import React from 'react';
import PropTypes from 'prop-types';
import getNotDeclaredProps from 'react-get-not-declared-props';
class Button extends React.Component {
static propTypes = {
children: PropTypes.node,
onPress: PropTypes.func,
};
handleClick = () => {
this.props.onPress();
};
render() {
return (
<div
onClick={this.handleClick}
// This will forward any props which are not specified inside propTypes
{...getNotDeclaredProps(this.props, Button)}
>
{this.props.children}
</div>
);
}
}