Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eslint - 'match' is missing in props validation (react/prop-types)

My eslint is failing on the following line.

  const id = this.props.match.params.personID;

How can I fix this. Setting a rule to ignore would be ok. Finding a fix would be better.

Error

 severity: 'Error'
    message: ''match' is missing in props validation (react/prop-types)'
    at: '14,27'
    source: 'eslint'
    code: 'react/prop-types'
like image 410
LeBlaireau Avatar asked Nov 27 '17 20:11

LeBlaireau


People also ask

How do I fix missing in props validation?

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.

Is missing in props validation ESLint disable?

To fix ESLint error missing in props validation with React, we can add a comment or disable the rule globally with a config. to disable prop type check for the line immediately below the comment in our code. in . eslintrc to disable the prop type validation rule for the whole project.

How do you validate props default props custom props validation in React?

React JS has an inbuilt feature for validating props data type to make sure that values passed through props are valid. React components have a property called propTypes which is used to setup data type validation. Syntax: The syntax to use propTypes is shown below. class Component extends React.

Do I need propTypes with TypeScript?

Props are required in TypeScript In the prop-types package, all props are optional by default. To make a prop required, you will have to use . isRequired explicitly.


1 Answers

When you are checking your prop types you also have to verify the shape of match.

If you are using flow:

type Props = {
  match: {
    params: {
      field1: number,
      field2: string,
    }
  }
  ...
}

class Component extends React.Component<Props> {
  ...
}

If you aren't and are using PropTypes...

import PropTypes from 'prop-types';

class MyComponent extends React.Component {
  ...
}

MyComponent.propTypes = {
  match: PropTypes.shape({
    params: PropTypes.shape({
      field1: PropTypes.number.isRequired
      field2: PropTypes.string
    })
  }),
  ...
}
like image 158
Dakota Avatar answered Oct 21 '22 10:10

Dakota