如何为高阶功能组件设置PropTypes?

时间:2017-10-27 08:35:44

标签: reactjs eslint react-proptypes higher-order-components eslint-config-airbnb

我使用airbnb配置进行eslint并且它给了我这个警告:

let dic = productArray[indexPath.row] as! NSDictionary let image = dic.object(forKey: "images") as! NSArray let imageUrl = image.value(forKey: "src") as! NSArray as! [String] cell.pinImage.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "pin1"))

有没有办法为isLoading设置PropTypes?

[eslint] 'isLoading' is missing in props validation (react/prop-types)

以下是我如何使用它的示例:

const withLoading = Component => ({ isLoading, ...rest }) =>
  (isLoading ? <LoadingSpinner /> : <Component {...rest} />);

我也将它发布到jsfiddle:http://jsfiddle.net/BernieLee/5kn2xa1j/36/

2 个答案:

答案 0 :(得分:7)

以下是您的需求:

const withLoading = (Component) => {
  const wrapped = ({ isLoading, ...rest }) => (
    isLoading ? <div>Loading</div> : <Component {...rest} />
  );
  wrapped.propTypes = {
    isLoading: PropTypes.bool.isRequired,
  };
  return wrapped;
};
withLoading.propTypes = {
  Component: PropTypes.element,
};

答案 1 :(得分:0)

猜猜isLoading是一个布尔值

withLoading.propTypes = {
    isLoading: React.PropTypes.bool
};