代码之家  ›  专栏  ›  技术社区  ›  Adeel Imran

Eslint react/prop类型与react/require默认props

  •  0
  • Adeel Imran  · 技术社区  · 5 年前

    有人能告诉我这两条规则的区别吗。

    react/prop-types react/require-default-props

    据我所知,他们都做同样的事情。

    0 回复  |  直到 5 年前
        1
  •  1
  •   truefalse10    5 年前

    仅在设置了道具类型时进行检查。

    反应/需要默认道具 强制开发人员为每个道具设置默认值。

    const HelloWorld = ({ name }) => (
      <h1>Hello, {name}!</h1>
    );
    
    // eslint react/prop-types will complain if you leave out this block
    HelloWorld.propTypes = {
      name: PropTypes.string
    };
    
    // eslint react/require-default-props checks for the following block
    HelloWorld.defaultProps = {
      name: 'john'
    };
    
    ReactDOM.render(<HelloWorld />,  document.getElementById('app'));
    
        2
  •  1
  •   Phillip    5 年前

    是的

    第一个是eslint规则,用于检查所使用的道具是否也在道具类型中定义。第二个规则是强制使用 不是 defaultProps