简体   繁体   English

ESLint React-Router v4 - 如何验证Match params props?

[英]ESLint React-Router v4 - How to validate Match params props?

I'm using React-Router v4's Match object to pass down params to the next component. 我正在使用React-Router v4的Match对象将params传递给下一个组件。 Where my Routing is like: 我的路由就像:

<Switch>
  <Route exact path="/" component={ExperimentListPage} />
  <Route path="/experiment/:experiment" component={ExperimentResultsPage} />
</Switch>

and my subcomponent looks like: 我的子组件看起来像:

const ExperimentResultsPage = props => (
   <ExperimentResultsContainer experimentName={props.match.params.experiment} />
);

export default withRouter(ExperimentResultsPage);

And it all works as intended, however ESLint is really unhappy with me using match.params.experiment and it errors with [eslint] 'match.params.experiment' is missing in props validation (react/prop-types) 这一切都按预期工作,但是ESLint对我使用match.params.experiment真的不满意,并且它[eslint] 'match.params.experiment' is missing in props validation (react/prop-types)错误

I saw in the React docs that I could use PropTypes.shape however my params object but I was hoping there is a better way especially because the Match object contains a lot of fields. 我在React文档中看到我可以使用PropTypes.shape但是我的params对象但是我希望有更好的方法,特别是因为Match对象包含很多字段。

Is there a better way of adding your Match route object to the props validation? 有没有更好的方法将Match路径对象添加到道具验证? How would that look like? 那会是什么样子? If not, am I missing some other approach that could help solve this? 如果没有,我是否错过了其他可以帮助解决此问题的方法?

I ran into something similar, eslint seems fine with only declaring what Im using as props and ignoring unused fields from match: 我遇到类似的东西,eslint似乎很好,只是声明我使用道具并忽略匹配中未使用的字段:

  match: PropTypes.shape({
    params: PropTypes.shape({
      experiment: PropTypes.string,
    }),
  }).isRequired,

在由此组件需要具有不同的形状的不同密钥的对象的情况下,着手,最好的方法是使用PropTypes.any内部objectOf如下:

 match: PropTypes.objectOf(PropTypes.any),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM