繁体   English   中英

React HOC ESLint 错误:禁止传播道具

[英]React HOC ESLint Error: props spreading is forbidden

是 eslint 不够聪明吗? HOC 非常通用,因此我无法真正指定传入的选项/道具,因为它是动态的,具体取决于此 HOC 在任何给定时间包装的组件。

props spreading is forbidden

在此处输入图像描述

withTracker.tsx

const withTracker = (WrappedComponent: any, options: any = {}) => {
    options.cookieDomain = 'xxxx';
    const trackPage = (page: any) => {
        GoogleAnalytics.set({
            page,
            ...options,
        });
        GoogleAnalytics.pageview(page);
    };

    const HOC = class HOC extends Component <{ location: any }> {
        componentDidMount() {
            const page = this.props.location.pathname;
            trackPage(page);
            window.scrollTo(0, 0);
        }

        componentDidUpdate(prevProps: any) {
            const currentPage = prevProps.location.pathname;
            const nextPage = this.props.location.pathname;

            if (currentPage !== nextPage) {
                trackPage(nextPage);
            }
        }

        render() {
            return <WrappedComponent {...this.props} />;
        }
    };

    return HOC;
};

export default withTracker;

只需禁用文件或特定行的 eslint 规则。

render(){
  // eslint-disable-next-line react/jsx-props-no-spreading
  return <WrappedComponent {...this.props} />
}

大概没有理由不能传播这些道具; 只是您的 ESLint 配置认为您不应该这样做。 该规则在文档中进行了描述; 这只是代码质量和风格的问题。 如果您想更改代码以符合规则,或者如果您不想或不能,请在行、文件或配置级别禁用规则。

暂无
暂无

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

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