【问题标题】:Eslint react/prop-types vs react/require-default-propsEslint react/prop-types vs react/require-default-props
【发布时间】:2019-11-22 08:20:03
【问题描述】:

谁能告诉我这两条 eslint 规则的区别。

react/prop-typesreact/require-default-props

据我了解,他们都做同样的事情。

【问题讨论】:

    标签: reactjs eslint


    【解决方案1】:

    react/prop-types 仅检查是否设置了 prop-types。

    react/require-default-props 强制开发人员为每个 prop 设置默认值。

    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】:

      他们做同样的事情。

      第一个是 eslint-rule 检查使用的 props 是否也在 prop-types 中定义。第二个是在defaultProps

      中设置不需要的props的执行规则

      【讨论】:

        猜你喜欢
        • 2017-11-15
        • 2018-09-15
        • 1970-01-01
        • 2020-10-30
        • 2021-07-27
        • 1970-01-01
        • 2020-11-12
        • 2015-09-06
        • 2021-08-26
        相关资源
        最近更新 更多