英文:
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.
英文:
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 validationeslintreact/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 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(
<>
<h1>{name}</h1>
<h1>{age}</h1>
</>
)}
//edit
even after spreading <SHow {...data}/>
it still showing
答案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
<SHow {...data} />
which is the equivalent of
<SHow name={name} age={age} />
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 "prop-types";
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:"name",age:"age"}
return(
<>
<SHow data={data}/>
</>
)}
function SHow({data}) {
return(
<>
<h1>{data.name}</h1>
<h1>{data.age}</h1>
</>
)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论