Eslint - 道具验证中没有'匹配'(反应/道具类型)

LeB*_*eau 5 properties reactjs eslint

我的意志在下一行失败了.

  const id = this.props.match.params.personID;
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题.将规则设置为忽略即可.找到一个解决方案会更好.

错误

 severity: 'Error'
    message: ''match' is missing in props validation (react/prop-types)'
    at: '14,27'
    source: 'eslint'
    code: 'react/prop-types'
Run Code Online (Sandbox Code Playgroud)

Dak*_*ota 11

当您检查您的道具类型时,您还必须验证匹配的形状.

如果您正在使用流程:

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

class Component extends React.Component<Props> {
  ...
}
Run Code Online (Sandbox Code Playgroud)

如果您不是并且正在使用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
    })
  }),
  ...
}
Run Code Online (Sandbox Code Playgroud)