繁体   English   中英

React + PropTypes和ESlint错误:如何验证回调的道具

[英]React + PropTypes and ESlint error: How to validate props of a callback

我有来自react-router这条Route

<Route
    path="/:url*"
    exact
    strict
    render={props => <Redirect to={`${props.location.pathname}/`} />}
/>

它只是在没有的路由后面加上斜线。

但ESLint上发出错误信号, props.location.pathname既是props.locationprops.location.pathname不与验证PropTypes

如何验证它们? 有办法吗?

注意:我不想忽略该规则:我想修复它以明确验证道具!

好的,所以按照@Tiago Coelho对我的问题的评论,以下是解决方案:

// RedirectToTrailingSlash.jsx
import React from 'react';
import PropTypes from 'prop-types';
import { Redirect } from 'react-router-dom';

const RedirectToTrailingSlash = props => (
  <Redirect to={`${props.location.pathname}/`} />
);

RedirectToTrailingSlash
  .propTypes = { location: PropTypes.shape({ pathname: PropTypes.string.isRequired }).isRequired };

export default RedirectToTrailingSlash;

这是改写的Route

// YourComponent.jsx
...
<Route
    path="/:url*"
    exact
    strict
    component={RedirectToTrailingSlash}
/>
...

暂无
暂无

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

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