反应道具类型验证错误

时间:2018-06-01 09:35:07

标签: reactjs validation react-proptypes

我有一个React组件,其中定义了以下proptypes:

NavBar.propTypes = {
  navbar: PropTypes.arrayOf(PropTypes.shape({
    brand: PropTypes.shape({
      linkTo: PropTypes.string,
      text: PropTypes.string,
    }),
    links: PropTypes.arrayOf(PropTypes.shape({
      linkTo: PropTypes.string,
      text: PropTypes.string,
      dropdown: PropTypes.bool,
      active: PropTypes.bool,
      links: PropTypes.links,
      eventKey: PropTypes.string,
    })),
  })),
};

道具验证给出以下错误:

20:50  error  'navbar.brand.linkTo' is missing in props validation  react/prop-types
20:83  error  'navbar.brand.text' is missing in props validation    react/prop-types
5:45  error  'linkTo' is missing in props validation  react/prop-types
5:60  error  'text' is missing in props validation    react/prop-types
6:47  error  'linkTo' is missing in props validation  react/prop-types
6:62  error  'text' is missing in props validation    react/prop-types
5:7   error  'active' is assigned a value but never used  no-unused-vars
6:23  error  'links' is missing in props validation       react/prop-types
6:29  error  'links.map' is missing in props validation   react/prop-types
17:42  error  'text' is missing in props validation        react/prop-types
7:23  error  'links' is missing in props validation      react/prop-types
7:29  error  'links.map' is missing in props validation  react/prop-types

我应该如何构建我的proptypes以免收到这些错误?

1 个答案:

答案 0 :(得分:1)

navbar验证问题:   您正在将navbar定义为数组,但看起来它正被用作对象。

linkTo验证问题:  您将linkTo嵌套在propsTypes定义的navbar.brandnavbar.links内,但看起来您像直接将其用作this.props.linksTo

text验证问题:  您将text嵌套在propsTypes定义的navbar.brandnavbar.links内,但看起来您像直接将其用作this.props.text

links验证问题:  您将links嵌套在propsTypes定义的navbar内,但看起来您像直接将其用作this.props.links

active验证问题:  您正在定义active,而不使用它。

如果您包括组件定义,我们可以提供更多的见解。