首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ESLint React PropTypes,prop验证中缺少'prop‘

ESLint React PropTypes,prop验证中缺少'prop‘
EN

Stack Overflow用户
提问于 2018-02-21 21:52:25
回答 1查看 15.8K关注 0票数 3

我有一个无状态的react组件

代码语言:javascript
复制
import React from 'react'
import styled from 'styled-components';
import PropTypes from 'prop-types';

export default props => <StyledButton>{props.children}</StyledButton>

StyledButton.propTypes = {
    children: PropTypes.node,
}
StyledButton.defaultProps = {
    children: null,
}

和一个类组件

代码语言:javascript
复制
class Thumbnail extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
           color: 'red',
        }
    }
    render() {
        return (
           <button>{this.props.children}</button>
       )
    }
}

Thumbnail.propTypes = {
    children: PropTypes.node,
}
Thumbnail.defaultProps = {
    children: null,
}

我的eslintrc文件

代码语言:javascript
复制
module.exports = {
"extends": "airbnb",
"plugins": [
    "react",
    "jsx-a11y",
    "import"
],
"rules": {
    "react/jsx-filename-extension": 0,
    "semi": 0,
    "indent": ["error", 4],
    "react/jsx-indent": 0,
    "jsx-a11y/img-redundant-alt": 0
}
};

Eslint抱怨说,在无状态组件中,“prop验证中缺少子项”。但它在class组件中很好用。

花了2个小时尝试解决这个问题,任何帮助都将不胜感激

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-21 22:19:47

这是因为您正在直接导出无状态组件,而没有任何保存它的变量,同时您正在为不存在的StyledButton创建propTypesdefaultProps。尝尝这个。

代码语言:javascript
复制
const StyledButton = props => <button>{props.children}</button>;

StyledButton.propTypes = {
  children: PropTypes.node,
};

StyledButton.defaultProps = {
  children: null,
};

export default StyledButton;
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48907661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档