反应匿名组件功能ESLint错误

时间:2018-10-25 22:34:10

标签: reactjs eslint

我需要帮助解决此React匿名函数上的这些皮棉错误。

export default {
  "text": () => { return <div className="item-icon">txt</div>; },
  "image": (props) => { return <img className="preview-img" src={props.src} alt=''/>; }
};

这是皮棉错误

  4:11  error  Component definition is missing display name  react/display-name
  5:12  error  Component definition is missing display name  react/display-name
  5:72  error  'src' is missing in props validation          react/prop-types

2 个答案:

答案 0 :(得分:0)

您可以添加评论eslint-disable,并且不会出现错误

/*eslint-disable */
export default {
  "text": () => { return <div className="item-icon">txt</div>; },
  "image": (props) => { return <img className="preview-img" src={props.src} alt=''/>; },
  "audio": () => { return <div className="item-icon">audio</div>; },
  "video": () => { return <div className="item-icon">video</div>; },
  "application": () => { return <div className="item-icon">{"< />"}</div>; },
  "application/pdf": () => { return <div className="item-icon">pdf</div>; },
};

答案 1 :(得分:0)

这解决了它们。

const text = () => { return <div className="item-icon">txt</div>; };
text.displayName = 'text';

const image = (props) => { return <img className="preview-img" src={props.src} alt=''/>; }
image.displayName = "image";
image.propTypes = {
  src: PropTypes.string
};

export default { text, image }