[英]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.location
和props.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.