【发布时间】:2021-07-27 05:16:46
【问题描述】:
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>
...
在我阅读 Airbnb JavaScript 样式指南之前,我的代码看起来如此。
然后我改成下面这样。
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>
我喜欢它,但是eslint指责我犯了以下错误:
'id' is missing in props validation react/prop-types
经过简短的研究,我发现了以下内容 React eslint error missing in props validation
所以我改变了我的代码。
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>
错误消失了,但后来我得到了这个错误:
Stateless functional components should not use `this` react/no-this-in-sfc
可能不是一个好的解决方案,我不使用类。
Airbnb 指南说以下是最好的方法。
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
但是如果括号中的参数超过 2 个怎么办?我记得 robert c martin 的干净代码书中提到了这一点。
什么对你有用?我也不想禁用任何规则,但想以干净的方式解决问题。
【问题讨论】:
标签: javascript reactjs eslint eslint-config-airbnb