【Typescript】项目编译没问题,eslint检查时报错:‘xxx‘ is missing in props validation react/prop-types

报错截图:
在这里插入图片描述
代码:
src/Child.tsx

type ChildProp = {
    count?: number
}

const Child: React.FC<ChildProp>= ({ count }) => {

    return <h3>{count}</h3>
}

export default Child;

原因是eslint在使用了"plugin:react/recommended"react检查时,会使用prop-types规则进行检查,在项目中使用的是ts,我们不会去引入prop-types去声明数据类型,所以会报错。这里有两种处理方法:

方法一

对组件和props都做类型声明

type ChildProp = {
    count?: number
}

const Child: React.FC<ChildProp>= ({ count }: ChildProp) => {

    return <h3>{count}</h3>
}

export default Child;

方法二
这是一劳永逸的做法。在eslint中,会默认使用react/prop-types检查,我们把它关掉就好了。
.eslintrc.js

"overrides": [
    {
        files: ["**/*.tsx"],
        // react默认使用prop-types来检查类型
        // 如果使用了typescript,就把这个关掉,
        // 不然会报一些没有意义的错误
        rules: {
            "react/prop-types": "off"
        }
    }
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值