问题
const propTypes = {
prop1: PropTypes.string,
prop2: PropTypes.string,
prop3: PropTypes.number,
};
const something = (props) => ((props.props2 > 0 & props.prop1 === props.props3) ?
t('translation/abc')
: t('translation/def'));
why does the component throw lint error PropType is defined but prop is never used
for all three props prop1, prop2, prop3
?
回答1:
This should fix your problem, let's de-structure your props and then assign propTypes to your stateless component.
const something = ({prop1, prop2, prop3}) => ((props2 > 0 & prop1 === props3)
? t('translation/abc')
: t('translation/def'));
something.propTypes = {
prop1: PropTypes.string,
prop2: PropTypes.string,
prop3: PropTypes.number,
};
回答2:
You are never assigning the propTypes constant to your function, so the props will not be validated against.
If you do
something.propTypes = propTypes;
the linter should accept your code.
来源:https://stackoverflow.com/questions/45527114/proptype-is-defined-but-prop-is-never-used