Destructured parameters causing eslint // {age, name} is missing in props

huangapple go评论65阅读模式
英文:

Destructured parameters causing eslint // {age, name} is missing in props

问题

I am using JavaScript. Destructuring like this will pick properties from the incoming argument object.

export default function App() {
    const data = { name: "name", age: "age" }

    return (
        <>
            <SHow data={data} />
        </>
    )
}

// eslint error 'age' is missing in props validation eslintreact/prop-types (parameter) age: any

function SHow({ name, age }) {
    // eslint error 'age' and 'age' is missing in props

    return (
        <>
            <h1>{name}</h1>
            <h1>{age}</h1>
        </>
    )
}

The code is working properly, so why ESLint is showing "eslint error 'age' and 'age' is missing in props"?

I tried this, but I don't want to create a new variable for each component.

function SHow(prop) {
    const { name, age } = prop;
    return (
        <>
            <h1>{name}</h1>
            <h1>{age}</h1>
        </>
    )
}

Even after spreading <SHow {...data} />, it still shows.

Destructured parameters causing eslint // {age, name} is missing in props

英文:

i am using javascript
destructuring like this will pick properties from the incoming argument object

export default function App() {
    const data={name:&quot;name&quot;,age:&quot;age&quot;}

    return(
        &lt;&gt;
           &lt;SHow data={data}/&gt;
        &lt;/&gt;
)}

////eslint error &#39;age&#39; is missing in props validationeslintreact/prop-types (parameter) age: any

function SHow({name,age}) {**/////eslint error &#39;age&#39; and  &#39;age&#39; is missing in props**

    return(
        &lt;&gt;
            &lt;h1&gt;{name}&lt;/h1&gt;
            &lt;h1&gt;{age}&lt;/h1&gt;
        &lt;/&gt;
)}

the code is working properly
so why es lint showing /////eslint error 'age' and 'age' is missing in props

i tryed this but i don't want create new variable for each component

function SHow(prop) {
    **const {name,age} =prop**
    return(
        &lt;&gt;
            &lt;h1&gt;{name}&lt;/h1&gt;
            &lt;h1&gt;{age}&lt;/h1&gt;
        &lt;/&gt;
)}

//edit
even after spreading &lt;SHow {...data}/&gt; it still showing
Destructured parameters causing eslint // {age, name} is missing in props

答案1

得分: 3

问题是您只传递了一个名为 data 的单个属性与原始值。

尝试要么扩展属性

<SHow {...data} />

这等同于

<SHow name={name} age={age} />

要么解构 data 属性

function SHow({ data: { name, age } }) {
  // ...
}

听起来您已经启用了 ESLint 中的 react/prop-types 规则,但根本没有定义 propTypes

React 不再建议 使用 propTypes...

PropTypes 在现代 React 中不常用。使用 TypeScript 进行静态类型检查。

对于您的 JavaScript 项目,我要么禁用规则,要么包含 prop-types 库 并相应地定义类型

import PropTypes from "prop-types";

function SHow({ name, age }) {
  // ...
}

SHow.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.string.isRequired,
};
英文:

The issue is you're only passing a single prop named data with the original.

Try either spreading the props

&lt;SHow {...data} /&gt;

which is the equivalent of

&lt;SHow name={name} age={age} /&gt;

or destructuring the data prop

function SHow({ data: { name, age } }) {
  // ...
}

Sounds like you've got the ESLint react/prop-types rule enabled but you're not defining propTypes at all.

React no longer recommends using propTypes...

> PropTypes aren’t commonly used in modern React. Use TypeScript for static type checking.

For your JavaScript project, I'd either disable the rule or include the prop-types library and define types accordingly

import PropTypes from &quot;prop-types&quot;;

function SHow({ name, age }) {
  // ...
}

SHow.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.string.isRequired,
};

答案2

得分: 1

你传递的属性是数据。尝试这样做:

export default function App() {
  const data = { name: "name", age: "age" };

  return (
    <>
      <SHow data={data} />
    </>
  );
}

function SHow({ data }) {
  return (
    <>
      <h1>{data.name}</h1>
      <h1>{data.age}</h1>
    </>
  );
}
英文:

The prop you are passing is data. Try this :

export default function App() {
const data={name:&quot;name&quot;,age:&quot;age&quot;}

return(
    &lt;&gt;
       &lt;SHow data={data}/&gt;
    &lt;/&gt;
)}

function SHow({data}) {

return(
    &lt;&gt;
        &lt;h1&gt;{data.name}&lt;/h1&gt;
        &lt;h1&gt;{data.age}&lt;/h1&gt;
    &lt;/&gt;
)}

huangapple
  • 本文由 发表于 2023年7月13日 17:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定