【问题标题】:is specifying isRequired in Proptypes should come at last?最后应该在 Proptypes 中指定 isRequired 吗?
【发布时间】:2017-07-26 11:10:18
【问题描述】:

我有一个简单的组件,我正在使用prop-types 包。

为了验证一个对象数组,我使用了类似的东西:

CoursesPage.propTypes = {
    courses: PropTypes.isRequired.arrayOf(PropTypes.shape({
        title: PropTypes.isRequired.string,
    })),
};

现在,Eslint react/require-default-props 给我一个错误,courses 属性是可选的,但没有默认值,但是如果 isRequired 出现在 arrayOf 属性之后,错误就消失了。

为了使一个道具成为必需的,是否需要最后规定isRequired属性?或者它是 Eslint react/require-default-prop 插件的错误?

【问题讨论】:

    标签: reactjs eslint react-proptypes


    【解决方案1】:

    isRequired 应该在数据类型之后 PropTypes.string.isRequiredyou can read more here

    你的情况

    CoursesPage.propTypes = {
        courses: PropTypes.arrayOf(PropTypes.shape({
            title: PropTypes.string,
        })).isRequired
    };
    

    【讨论】:

    • 你确定吗?我没有看到有关此要求的提及。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多