Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react/prop-types eslint error in typescript react component

I am trying to set up a typescript-react-eslint project and can't get past eslint error for this boilerplate component:

import * as React from "react";

interface ButtonProps {
  children?: React.ReactNode,
  onClick?: (e: any) => void,
}

const styles = {
  border: "1px solid #eee",
  borderRadius: 3,
  backgroundColor: "#FFFFFF",
  cursor: "pointer",
  fontSize: 15,
  padding: "3px 10px",
  margin: 10
};

const Button: React.FunctionComponent<ButtonProps> = props => (
  <button onClick={props.onClick} style={styles} type="button">
    {props.children}
  </button>
);

Button.defaultProps = {
  children: null,
  onClick: () => {}
};
export default Button;

The error is:

  19:26  error  'onClick' is missing in props validation   react/prop-types
  20:12  error  'children' is missing in props validation  react/prop-types

It seems like it is complaining about interface for html <button> is not defined? Otherwise it might be the Button component itself but should it not get type information from <ButtonProps> interface I pass there?

I tried explicitly setting children and onClick like this:

Button.propTypes = {
  children?: React.ReactNode,
  onClick?: (e: any) => void
};

it bypasses the eslint error but the component itself stops working. What am I doing wrong?

P.S. This is my .eslintrc.json

{
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/eslint-recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "settings": {
        "react": {
            "pragma": "React",
            "version": "detect"
        }
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "react",
        "@typescript-eslint"
    ],
    "rules": {
        "indent": [
            "error",
            2
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "double"
        ],
        "semi": [
            "error",
            "always"
        ]
    }
}
like image 438
MaxPY Avatar asked Dec 15 '19 23:12

MaxPY


People also ask

How to fix the ‘react ESLint error missing in props validation’ when developing?

Fix the ‘React eslint error missing in props validation’ When Developing a React App? To fix the ‘React eslint error missing in props validation’ when developing a React app, we can set the prop types of the props in the component causing the error. to import the prop-types package to let us add prop type validation to the Foo component.

What is lint error in react Lint?

The lint message: error Missing an explicit type attribute for button react/button-has-type. Not specifying attributes in certain components can produce lint error messages.

How to type props in a blog post?

As you can see and recall, it basically equals an object, and you can refer to its values ( props.title and props.subtitle) just like any other object, therefore if you use TypeScript, you can type them as well. Typically, programmers who only use TypeScript because it’s popular type props as follows: const BlogPostComponent = (props: any) => ...

Does mouseevent create a compile-time error in react?

It won’t create a compile-time error, but lint will find the error or properties won’t be available. React uses its own event system. That’s why we can’t use typically MouseEvent from standard DOM.


Video Answer


4 Answers

I ended up rewriting the component as:

const Button = ({ children, onClick }: ButtonProps) => {
  return <button onClick={onClick} style={styles} type="button">
    {children}
  </button>;
};

The : React.FC<ButtonProps> part was ignored by eslint so I decided to provide prop types in a more straightforward way

like image 105
MaxPY Avatar answered Oct 03 '22 14:10

MaxPY


This rule doesn't make sense with TypeScript because you already is checking types.

In this question you found a simple way to disable this rule, just add in your eslint configuration:

  rules: {
    'react/prop-types': 0
  }

to be more readable you can use "off" instead "0".

like image 31
João Bispo Avatar answered Oct 03 '22 14:10

João Bispo


eslint-plugin-react@^7.25.0 appears to have resolved the issue for those using React.FC<IProps> with react/prop-types validation rule.

So instead of

const Example: React.FC<IProps> = (props: IProps) => ...

This now works without warnings after the update

const Example: React.FC<IProps> = (props) => ...
like image 27
Sunnyok Avatar answered Oct 03 '22 14:10

Sunnyok


More info to your answer..

Firstly both ways are correct for declaring types, But React.FC has some added benefits. https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/blob/master/README.md#function-components

enter image description here

And in your scenario you may be using eslint-react-plugin which has recommended rules 'plugin:react/recommended' for eslint ,
Rule to check proptypes is one among them, check typescript example. https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prop-types.md

So react/prop-types rule will conflict with TS Interfaces, which is why it shows that error, once you add : ButtonProps, we don't have to provide React.FC

like image 40
Hemanthvrm Avatar answered Oct 02 '22 14:10

Hemanthvrm