首页 > 解决方案 > 如何在此处定义 proptypes

问题描述

我无法避免Eslint 错误(“道具类型验证中缺少组件”)。

如何定义proptypes

  1. 零件
  2. 地点
function PrivateRoute({ component: Component, ...rest }) {
  return (
    <Route
      {...rest}
      render={props =>
        cookies.get("token") !== "" && cookies.get("token") !== undefined ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/401",
              state: { from: props.location }
            }}
          />
        )
      }
    />
  );
}
PrivateRoute.prototype = {
  component: PropTypes.objectOf(React.Component)
};
Redirect.prototype = {
  location: PropTypes.string
};

标签: reactjs

解决方案


你有一些拼写错误,使用prototype而不是propTypes

import PropTypes from 'prop-types';

PrivateRoute.propTypes = {
  component: PropTypes.objectOf(React.Component)
};

更多 propTypes 信息在这里


推荐阅读