简体   繁体   English

Object 解构 eslint 抛出 react/prop-types

[英]Object Destructuring eslint throws react/prop-types

export const Top = (props) => {

    return (
        <div key={props.id} className={props.borderColor}>
            <div className="h-3/4 bg-green-200 border-black border-1 grid justify-items-center">
                <p className="text-xl font-bold">{props.title}</p>
...

My code looked so until I read the Airbnb JavaScript Style Guide.在我阅读 Airbnb JavaScript 样式指南之前,我的代码看起来如此。

Then I changed it to the following.然后我将其更改为以下内容。

export const Top = (props) => {
    const { id, borderColor, title, price, color } = props;
    
    return (
        <div key={id} className={borderColor}>
            <div className="h-3/4 bg-green-200 border-black border-1 grid justify-items-center">
                <p className="text-xl font-bold">{title}</p>

I liked it, but eslint accused me of the following error:我喜欢它,但 eslint 指责我犯了以下错误:

'id' is missing in props validation           react/prop-types

After a short research I came across the following React eslint error missing in props validation经过简短的研究后,我在道具验证中遇到了以下 React eslint 错误

So I have changed my code.所以我改变了我的代码。

export const Top = () => {
    const { id, borderColor, title, price, color } = this.props;

    return (
        <div key={id} className={borderColor}>
            <div className="h-3/4 bg-green-200 border-black border-1 grid justify-items-center">
                <p className="text-xl font-bold">{title}</p>

The error went away, but then i got this error:错误消失了,但后来我收到了这个错误:

Stateless functional components should not use `this`  react/no-this-in-sfc

Probably not a good solution and I don't use classes.可能不是一个好的解决方案,我不使用类。

The Airbnb Guidlines say the following is the best way to go. Airbnb 指南说以下是 go 的最佳方式。

function getFullName({ firstName, lastName }) {
  return `${firstName} ${lastName}`;
}

But what if you have more than 2 parameters in the parentheses?但是如果括号中的参数超过 2 个怎么办? I remember this being talked about as bad in the clean code book by robert c martin.我记得 robert c martin 的干净代码书中谈到了这一点。

What works well for you?什么适合你? I also don't want to disable any rules, but would like to solve the problem in a clean way.我也不想禁用任何规则,但想以干净的方式解决问题。

export const Top = (props) => {
const { id, borderColor, title, price, color } = props;

return (
    <div key={id} className={borderColor}>
        <div className="h-3/4 bg-green-200 border-black border-1 grid justify-items-center">
            <p className="text-xl font-bold">{title}</p>

This was right.这是对的。 Just you have to do some extra code.只是你必须做一些额外的代码。

  • Before export block you have to import PropTypes在导出块之前,您必须导入PropTypes

     import React from 'react'; import PropTypes from 'prop-types';
  • Then after export block you have to add these然后在导出块之后你必须添加这些

    Top.propTypes = { id: PropTypes.oneOfType(PropTypes.number, PropTypes.string), borderColor: PropTypes.string, title: PropTypes.string, price: PropTypes.number, color: PropTypes.string, }

For more details, checkout this link https://reactjs.org/docs/typechecking-with-proptypes.html有关更多详细信息,请查看此链接https://reactjs.org/docs/typechecking-with-proptypes.html

you got the second error (with this ) becuase there is no this in function component你得到了第二个错误(有this ),因为 function 组件中没有this

try:尝试:

export const Top = (props) => {
    const { id, borderColor, title, price, color } = props;
...

you can get the component's props using this methods您可以使用此方法获取组件的道具
1) 1)

export default MyComponent(props){
...
}
  1. destructure the props object解构道具 object
export default MyComponent({prop1, prop2, prop3} ...){
...
}

i don't like the the second option because if tomorrow you will add 10 new props you need to make sure you added this to the parentheses and after that the parentheses will look much much longer then before which makes it unreadable我不喜欢第二个选项,因为如果明天你将添加 10 个新道具,你需要确保将其添加到括号中,之后括号看起来会比之前长得多,这使得它不可读

but if it is a small component the second option is the best但如果它是一个小组件,第二个选项是最好的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM