PropType is defined but prop is never used

拟墨画扇 提交于 2019-12-12 17:01:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!