9

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'
1

1 Answer 1

16

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
    })
  }),
  ...
}
3
  • 1
    Do you think this is the correct way to do it? Alternatively I got "react/prop-types": 0 in my eslint to work.
    – LeBlaireau
    Commented Nov 27, 2017 at 21:41
  • This actually type checks match whereas "react/prop-types": 0 just disables prop type checking in eslint.
    – Dakota
    Commented Nov 27, 2017 at 21:43
  • 1
    Does not work anymore with React-router v4 : even with path-to-regexp the parameter is not converted to an integer. ex: <Route exact path="/user/:id(\d+)" ... /> your PropTypes validation will return: Failed prop type: Invalid prop 'id' of type 'string'. Correct me if I missed something (and I'd like to as having to convert beforehand is just boilerplate)
    – Mose
    Commented Aug 3, 2018 at 8:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.